【Android】利用WindowsManager悬浮窗播放本地视频以及下载线上视频保存在本地

基于目前又重新拾起了分屏的项目需求,对之前研究的分屏播放视频做了更深入的研究。在之前的基础上做了改进和用户优化上的处理,实现了原生的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' ```

项目重新整理优化了之后变得更加简洁了,并且使得下载视频文件到加载悬窗视频播放的逻辑更加清晰,如果有什么其他的更好地处理方法,希望能告知。如果有需要采用我这种处理方式的可以在下方下载demo

demo: https://download.csdn.net/download/d38825/10381189

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值