基于目前又重新拾起了分屏的项目需求,对之前研究的分屏播放视频做了更深入的研究。在之前的基础上做了改进和用户优化上的处理,实现了原生的VideoView加载本地视频。并使用FileDownLoader下载大文件的库来将线上视频文件下载到本地。
1.必要的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_TASKS" />
<!-- SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 向SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.悬浮窗View,实现视频的加载以及视频文件的循环播放
public class FloatWindowView extends LinearLayout {
/**
* 记录悬浮窗的宽度
*/
public static int viewWidth;
/**
* 记录悬浮窗的高度
*/
public static int viewHeight;
private Context mContext;
private VideoView videoView;
public FloatWindowView(final Context context) {
super(context);
mContext = context;
LayoutInflater.from(context).inflate(R.layout.float_window, this);
View view = findViewById(R.id.big_window_layout);
viewWidth = view.getLayoutParams().width;
viewHeight = view.getLayoutParams().height;
videoView = this.findViewById(R.id.video_view);
if (!utils.fileIsExists(MainActivity.path + "/" + MainActivity.videoName)) {
Log.e("TAG", "播放路径不存在!");
//可以加载项目中资源文件里面的默认视频
return;
}
videoView.setVideoPath(MainActivity.path + "/" + MainActivity.videoName);
videoView.setZOrderOnTop(true);
videoView.setZOrderMediaOverlay(true);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.e("TAG", "onPrepared");
mp.start();
mp.setLooping(true);
Intent intentHome = new Intent(Intent.ACTION_MAIN);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.addCategory(Intent.CATEGORY_HOME);
mContext.startActivity(intentHome);
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.e("TAG", "onCompletion");
videoView.setVideoPath(MainActivity.path + "/" + MainActivity.videoName);
videoView.start();
}
});
videoView.start();
}
}
从上面的播放视频的悬浮窗View代码来看,主要有几点需要注意的。第一是需要先判断视频路径是否存在,否则会弹窗提示无法播放视频;第二是实现视频的循环播放,需要实现两个监听方法来处理播放事件。在onPrepared方法中处理了模拟home键的点击事件,来退出MainActivity的界面,只保留视频广告悬窗。
3.悬浮窗布局float_window
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/big_window_layout"
android:layout_width="match_parent"
android:layout_height="700dip"
android:background="@color/white"
android:orientation="vertical">
<VideoView
android:id="@+id/video_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
这里也有几个坑需要注意,就是有时候悬浮窗无法充满父布局的情况下,我们需要如上xml中进行处理。保证父布局是RelativeLayout,VideoView需要alignParent各个方向。
4.保持悬浮窗运行的service
public class FloatWindowService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyWindowManager.createWindow(MainActivity.getInstance());
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
5.悬浮窗管理器
public class MyWindowManager {
/**
* 悬浮窗View的实例
*/
private static FloatWindowView mWindow;
/**
* 悬浮窗View的参数
*/
private static WindowManager.LayoutParams mWindowParams;
/**
*
* @param context 必须为应用程序的Context.
*/
public static void createWindow(Context context) {
WindowManager windowManager = (WindowManager)
context.getApplicationContext().getSystemService("window");
if (mWindow == null) {
mWindow = new FloatWindowView(context);
if (mWindowParams == null) {
mWindowParams = new WindowManager.LayoutParams();
mWindowParams.x = 0;
mWindowParams.y = 0;
mWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWindowParams.format = PixelFormat.RGBA_8888;
mWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
}
mWindow.setFitsSystemWindows(true);
mWindow.getWindowToken();
windowManager.addView(mWindow, mWindowParams);
}
}
}
6.MainActivity
public class MainActivity extends Activity {
public static final String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/miniVideo";
public static final String videoName = "asc.mp4";
private static MainActivity instance;
private String videoUrl = "http://192.168.0.177/Courierark/Apk/" + videoName;
public static synchronized MainActivity getInstance() {
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
setContentView(R.layout.activity_main);
//检查并下载视频资源
checkAndDownLoadVideo();
}
/**
* 检查并下载视频资源
*/
private void checkAndDownLoadVideo() {
File file = new File(path);
Log.e("TAG", "===>" + path);
//文件夹不存在,则创建它
if (!file.exists()) {
file.mkdir();
}
FileDownloader.setup(this);
FileDownloader.getImpl().create(videoUrl)
.setPath(path + "/" + videoName)
.setForceReDownload(false)
.setListener(new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.e("TAG", "pending");
}
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
Log.e("TAG", "connected");
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.e("TAG", "progress");
}
@Override
protected void blockComplete(BaseDownloadTask task) {
Log.e("TAG", "blockComplete");
}
@Override
protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
Log.e("TAG", "retry");
}
@Override
protected void completed(BaseDownloadTask task) {
Log.e("TAG", "completed");
Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
startService(intent);
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.e("TAG", "paused");
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
Log.e("TAG", "error===>" + e.getMessage());
}
@Override
protected void warn(BaseDownloadTask task) {
Log.e("TAG", "warn");
}
}).start();
}
}
FileDownLoader第三方库引入:
compile 'com.liulishuo.filedownloader:library:1.7.1'