0. 现完成功能:
- 悬浮窗区分横屏竖屏两种尺寸
- 悬浮窗可以在页面上随意拖动
- 在播放视频时按返回键/Home键/离开当前页时触发开启
- 悬浮窗显示在退到后台/在应用内/桌面
- 带播放进度开启悬浮窗,带播放进度回到应用内页面
- 权限:每次开启前判断有无权限,没权限并且请求过就不开悬浮窗
已通过现有机型测试:三星、华为、vivo
1.引入依赖
//完整版引入
implementation 'com.shuyu:GSYVideoPlayer:8.1.2'
github:https://github.com/CarGuo/GSYVideoPlayer(强烈建议下载Demo源码,每个方法注释写的很清晰)
踩坑1:部分依赖时 打包apk一播放视频就崩溃,连接真机正常播放。报错是aliplay原因,注释掉换一个播放器内核:
implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-java:v8.3.4-release-jitpack'
//implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer-aliplay:8.3.4-release-jitpack'
PlayerFactory.setPlayManager(SystemPlayerManager.class); // 换个系统内核
2.清单文件声明悬浮窗权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
3.从Demo源码导出需要的15个文件加入自己项目,并进行部分修改
1)修改悬浮窗布局 layout_floating_video,加上删除和回到应用的icon,点击播放/暂停的区域
layout_floating_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/surface_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</FrameLayout>
<LinearLayout
android:id="@+id/ll_center"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center">
<moe.codeest.enviews.ENPlayView
android:id="@+id/start"
android:layout_width="35dp"
android:layout_height="35dp" />
</LinearLayout>
<ImageView
android:id="@+id/iv_delete"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_alignParentLeft="true"
android:padding="9dp"
android:src="@drawable/video_small_close" />
<ImageView
android:id="@+id/iv_detail"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_alignParentRight="true"
android:padding="12dp"
android:src="@drawable/video_enlarge" />
</RelativeLayout>
FloatingVideo.java
public class FloatingVideo extends StandardGSYVideoPlayer {
//... 以下新添
//用于跳转回对应页面
public int newsId;
private NewsDetailActivity activity;
public FloatingVideo(Context context, int newsId, LaunchActivity activity){
super(context);
this.newsId = newsId;
this.activity = activity;
}
@Override
protected void init(Context context)</