From 626300f116874670b2cc7df6c0fcc11a4301a9c4 Mon Sep 17 00:00:00 2001 From: guoshuyu Date: Tue, 16 Jan 2018 16:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96SmallVideoHelper=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../gsyvideoplayer/ListVideo2Activity.java | 4 +- .../adapter/ListVideoAdapter.java | 3 +- .../utils/SmallVideoHelper.java | 84 ++++++++++++++----- app/src/main/res/layout/activity_main.xml | 4 +- 5 files changed, 73 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 6a99d86..8dba6ed 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ compile 'com.shuyu:gsyVideoPlayer-ex_so:3.0.0' -5、DEMO增加SamllVideoHelper实现小窗口逻辑,更新demo +5、DEMO增加SmallVideoHelper实现小窗口,全屏逻辑,更新demo diff --git a/app/src/main/java/com/example/gsyvideoplayer/ListVideo2Activity.java b/app/src/main/java/com/example/gsyvideoplayer/ListVideo2Activity.java index 6c35a92..e82f76b 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/ListVideo2Activity.java +++ b/app/src/main/java/com/example/gsyvideoplayer/ListVideo2Activity.java @@ -59,7 +59,9 @@ public class ListVideo2Activity extends AppCompatActivity { //创建小窗口帮助类 smallVideoHelper = new SmallVideoHelper(this); - smallVideoHelper.setFullViewContainer(videoFullContainer); + + //如果不设置即使用默认的 windowViewContainer + //smallVideoHelper.setFullViewContainer(videoFullContainer); //配置 gsySmallVideoHelperBuilder = new SmallVideoHelper.GSYSmallVideoHelperBuilder(); diff --git a/app/src/main/java/com/example/gsyvideoplayer/adapter/ListVideoAdapter.java b/app/src/main/java/com/example/gsyvideoplayer/adapter/ListVideoAdapter.java index 13908b1..01d9226 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/adapter/ListVideoAdapter.java +++ b/app/src/main/java/com/example/gsyvideoplayer/adapter/ListVideoAdapter.java @@ -94,7 +94,8 @@ public class ListVideoAdapter extends BaseAdapter { notifyDataSetChanged(); //listVideoUtil.setLoop(true); smallVideoHelper.setPlayPositionAndTag(position, TAG); - final String url = "https://res.exexm.com/cw_145225549855002"; + //final String url = "https://res.exexm.com/cw_145225549855002"; + final String url = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"; gsySmallVideoHelperBuilder.setVideoTitle("title " + position) .setUrl(url); smallVideoHelper.startPlay(); diff --git a/app/src/main/java/com/example/gsyvideoplayer/utils/SmallVideoHelper.java b/app/src/main/java/com/example/gsyvideoplayer/utils/SmallVideoHelper.java index 48cf13a..f35461f 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/utils/SmallVideoHelper.java +++ b/app/src/main/java/com/example/gsyvideoplayer/utils/SmallVideoHelper.java @@ -9,6 +9,7 @@ import android.os.Handler; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; +import android.view.Window; import android.widget.FrameLayout; import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder; @@ -51,6 +52,10 @@ public class SmallVideoHelper { * 全屏承载布局 */ private ViewGroup fullViewContainer; + /** + * 全屏承载布局 + */ + private ViewGroup windowViewContainer; /** * 记录列表中item的父布局 */ @@ -102,13 +107,14 @@ public class SmallVideoHelper { public SmallVideoHelper(Context context) { - gsyVideoPlayer = new StandardGSYVideoPlayer(context); - this.context = context; + this(context, new StandardGSYVideoPlayer(context)); } public SmallVideoHelper(Context context, StandardGSYVideoPlayer player) { gsyVideoPlayer = player; this.context = context; + this.windowViewContainer = (ViewGroup) (CommonUtil.scanForActivity(context)).findViewById(Window.ID_ANDROID_CONTENT); + } /** @@ -156,11 +162,17 @@ public class SmallVideoHelper { * 添加到全屏父布局里 */ private void resolveFullAdd() { - if(gsyVideoOptionBuilder.isShowFullAnimation()) { - fullViewContainer.setBackgroundColor(Color.BLACK); + if (gsyVideoOptionBuilder.isShowFullAnimation()) { + if (fullViewContainer != null) { + fullViewContainer.setBackgroundColor(Color.BLACK); + } } resolveChangeFirstLogic(0); - fullViewContainer.addView(gsyVideoPlayer); + if (fullViewContainer != null) { + fullViewContainer.addView(gsyVideoPlayer); + } else { + windowViewContainer.addView(gsyVideoPlayer); + } } /** @@ -176,12 +188,20 @@ public class SmallVideoHelper { FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(listItemSize[0], listItemSize[1]); lp.setMargins(listItemRect[0], listItemRect[1], 0, 0); frameLayout.addView(gsyVideoPlayer, lp); - fullViewContainer.addView(frameLayout, lpParent); + if (fullViewContainer != null) { + fullViewContainer.addView(frameLayout, lpParent); + } else { + windowViewContainer.addView(frameLayout, lpParent); + } handler.postDelayed(new Runnable() { @Override public void run() { //开始动画 - TransitionManager.beginDelayedTransition(fullViewContainer); + if (fullViewContainer != null) { + TransitionManager.beginDelayedTransition(fullViewContainer); + } else { + TransitionManager.beginDelayedTransition(windowViewContainer); + } resolveMaterialFullVideoShow(gsyVideoPlayer); resolveChangeFirstLogic(600); } @@ -207,20 +227,25 @@ public class SmallVideoHelper { */ private void resolveToNormal() { int delay = orientationUtils.backToProtVideo(); - if(!gsyVideoOptionBuilder.isShowFullAnimation()) { + if (!gsyVideoOptionBuilder.isShowFullAnimation()) { delay = 0; } handler.postDelayed(new Runnable() { @Override public void run() { isFull = false; - fullViewContainer.removeAllViews(); + removeWindowContainer(); + if (fullViewContainer != null) { + fullViewContainer.removeAllViews(); + } if (gsyVideoPlayer.getParent() != null) { ((ViewGroup) gsyVideoPlayer.getParent()).removeView(gsyVideoPlayer); } orientationUtils.setEnable(false); gsyVideoPlayer.setIfCurrentIsFullscreen(false); - fullViewContainer.setBackgroundColor(Color.TRANSPARENT); + if (fullViewContainer != null) { + fullViewContainer.setBackgroundColor(Color.TRANSPARENT); + } listParent.addView(gsyVideoPlayer, listParams); gsyVideoPlayer.getFullscreenButton().setImageResource(gsyVideoPlayer.getEnlargeImageRes()); gsyVideoPlayer.getBackButton().setVisibility(View.GONE); @@ -274,15 +299,26 @@ public class SmallVideoHelper { */ private void resolveChangeFirstLogic(int time) { if (gsyVideoOptionBuilder.isLockLand()) { - handler.postDelayed(new Runnable() { - @Override - public void run() { - if (orientationUtils.getIsLand() != 1) { + if (time > 0) { + handler.postDelayed(new Runnable() { + @Override + public void run() { + if (orientationUtils.getIsLand() != 1) { + if (fullViewContainer != null) { + fullViewContainer.setBackgroundColor(Color.BLACK); + } + orientationUtils.resolveByClick(); + } + } + }, time); + } else { + if (orientationUtils.getIsLand() != 1) { + if (fullViewContainer != null) { fullViewContainer.setBackgroundColor(Color.BLACK); - orientationUtils.resolveByClick(); } + orientationUtils.resolveByClick(); } - }, time); + } } gsyVideoPlayer.setIfCurrentIsFullscreen(true); if (gsyVideoOptionBuilder.getVideoAllCallBack() != null) { @@ -320,6 +356,14 @@ public class SmallVideoHelper { return isPlayView(position, tag); } + private boolean removeWindowContainer() { + if (windowViewContainer != null && windowViewContainer.indexOfChild(gsyVideoPlayer) != -1) { + windowViewContainer.removeView(gsyVideoPlayer); + return true; + } + return false; + } + /** * 动态添加视频播放 * @@ -402,9 +446,6 @@ public class SmallVideoHelper { * 全屏按键逻辑 */ public void doFullBtnLogic() { - if (fullViewContainer == null) { - return; - } if (!isFull) { resolveToFull(); } else { @@ -421,6 +462,9 @@ public class SmallVideoHelper { if (fullViewContainer != null && fullViewContainer.getChildCount() > 0) { isFull = true; resolveMaterialToNormal(gsyVideoPlayer); + } else if (windowViewContainer != null && windowViewContainer.indexOfChild(gsyVideoPlayer) != -1) { + isFull = true; + resolveMaterialToNormal(gsyVideoPlayer); } return isFull; } @@ -429,6 +473,7 @@ public class SmallVideoHelper { * 释放持有的视频 */ public void releaseVideoPlayer() { + removeWindowContainer(); ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent(); if (viewGroup != null) viewGroup.removeAllViews(); @@ -465,6 +510,7 @@ public class SmallVideoHelper { /** * 设置全屏显示的viewGroup + * 如果不设置即使用默认的 windowViewContainer * * @param fullViewContainer viewGroup */ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9c196fe..828434f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -61,7 +61,7 @@ android:layout_width="240dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" - android:text="List小窗口列表" /> + android:text="List全屏和小窗口列表" />