饺子播放器、IjkVideoView播放器的简单使用

饺子播放器
一、加入依赖

implementation 'cn.jzvd:jiaozivideoplayer:6.2.4'

二、视频横竖屏切换 需要在清单文件中设置属性

<activity
            android:name=".JiaoZiplayerActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:screenOrientation="portrait" />

三、播放

private void initView() {

        jps.setUp("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
                JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                "狮子王");
        jps.thumbImageView.setImageURI(Uri.parse("https://images0.cnblogs.com/blog/651487/201501/281616176915467.jpg"));
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap bitmap = createVideoThumbnail("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", jps.thumbImageView.getMaxWidth(),jps.thumbImageView.getMaxHeight());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        jps.thumbImageView.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();

        jps.widthRatio = 4;
        jps.heightRatio = 3;

        //设置全屏播放
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;  //横向
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;  //纵向

    }

四、如果需要视频封面 可以封装工具类 截取特定帧

 //获取视频的第一帧作为视频的缩略图(网络视频):
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private Bitmap createVideoThumbnail(String url, int width, int height) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                retriever.setDataSource(url, new HashMap<String, String>());
            } else {
                retriever.setDataSource(url);
            }
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            // 假设这是一个损坏的视频文件
        } catch (RuntimeException ex) {
            //假设这是一个损坏的视频文件
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // 清理时忽略故障
            }
        }
        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

五、健壮性 及时清理

 @Override
    public void onBackPressed() {
        if (JZVideoPlayerStandard.backPress()) {
            return;

        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayerStandard.releaseAllVideos();
    }

IjkVideoView播放器
一、加入依赖

implementation 'com.dou361.ijkplayer:jjdxm-ijkplayer:1.0.5'
``
**二、写入代码 有需求可以自行更改**
```javascript
 View rootView = getLayoutInflater().from(this).inflate(R.layout.simple_player_view_player, null);
        setContentView(rootView);


         playerView = new PlayerView(this)
                .setScaleType(PlayStateParams.fitparent)
                .setPlaySource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
                .setTitle("标题")
                .startPlay();

你可能感兴趣的:(android,android)