Android视频播放器——DKVideoPlayer
DKVideoPlayer介绍:
1. 标星5k的Android视频播放器,支持原生VideoView组件核心,Bilibili的IjkPlayer组件核心,ExoPlayer组件核心,可切换核心
2. 视频组件自带播放暂停,进度条,全屏,播放开始,播放结束,重新播放等ui样式。也可自己修改界面样式和功能
3. 可设置静音,轮播,倍速,小窗,锁定播放界面,边播边缓存,视频截图,连续播放视频集,调整显示比例,或无组件纯视频播放
4. 支持raw,assert,file,url
5. 可播放在线视频流
DKVideoPlayer地址:https://github.com/Doikki/DKVideoPlayer
坐标:
# 必选,内部默认使用系统mediaplayer进行解码
implementation 'xyz.doikki.android.dkplayer:dkplayer-java:3.3.7'
# 可选,包含StandardVideoController的实现
implementation 'xyz.doikki.android.dkplayer:dkplayer-ui:3.3.7'
# 可选,使用exoplayer进行解码
implementation 'xyz.doikki.android.dkplayer:player-exo:3.3.7'
# 可选,使用ijkplayer进行解码
implementation 'xyz.doikki.android.dkplayer:player-ijk:3.3.7'
# 可选,如需要缓存或者抖音预加载功能请引入此库
implementation 'xyz.doikki.android.dkplayer:videocache:3.3.7'
使用:
1. 基本使用:
1.1添加布局:
<xyz.doikki.videoplayer.player.VideoView android:id="@+id/player" android:layout_width="match_parent" android:layout_height="300dp" />
1.2设置视频地址和控制器:
//videoView.setUrl("https://www.baidu.com/a.mp4") //播放网络视频
//videoView.setUrl("android.resource://" + getPackageName() + "/" + R.raw.movie") //播放raw文件里视频资源,连接格式 android.resourcde://com.dueeeke.dkplayer/"+R.raw.moveid
//videoView.setUrl("file:///android_asset/test.mp4") //播放assert里的视频 连接格式 file://android_asset/test.mp4
//videoView.setUrl("/sdcard/Alarms/xxx.mp4") //播放安卓本地路径视频 /sdcard/xxx
videoView.setUrl(String url); //设置视频地址
StandardVideoController controller = new StandardVideoController(this); controller.addDefaultControlComponent("标题", false); //controller设置为默认控制器 videoView.setVideoController(controller); //设置控制器 videoView.start(); //开始播放,不调用则不自动播放
1.3播放生命周期:(implements xyz.doikki.videoplayer.player.VideoView.OnStateChangeListener)
@Override public void onPlayerStateChanged(int playerState) { switch (playerState) { case VideoView.PLAYER_NORMAL://小屏 break; case VideoView.PLAYER_FULL_SCREEN://全屏 break; } } @Override public void onPlayStateChanged(int playState) { switch (playState) { case VideoView.STATE_IDLE: break; case VideoView.STATE_PREPARING: //准备播放中 //在STATE_PREPARING时设置setMute(true)可实现静音播放 //mVideoView.setMute(true); break; case VideoView.STATE_PREPARED: //准备播放后 break; case VideoView.STATE_PLAYING: //播放中 //需在此时获取视频宽高 int[] videoSize = mVideoView.getVideoSize(); L.d("视频宽:" + videoSize[0]); L.d("视频高:" + videoSize[1]); break; case VideoView.STATE_PAUSED: //播放暂停 break; case VideoView.STATE_BUFFERING: break; case VideoView.STATE_BUFFERED: break; case VideoView.STATE_PLAYBACK_COMPLETED: //播放完成 break; case VideoView.STATE_ERROR: break; } }
videoView.addOnStateChangeListener(this); //添加该播放状态监听器
1.4在Activity中
@Override protected void onPause() { super.onPause(); videoView.pause(); } @Override protected void onResume() { super.onResume(); videoView.resume(); } @Override protected void onDestroy() { super.onDestroy(); videoView.release(); } @Override public void onBackPressed() { if (!videoView.onBackPressed()) { super.onBackPressed(); } }
<!-- AndroidManifest.xml -->
<activity
android:name=".PlayerActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait" /> <!-- or android:screenOrientation="landscape"-->
完整代码
public class DKVideoPlayerActivity extends AppCompatActivity implements View.OnClickListener ,VideoView.OnStateChangeListener { private String uri = "android.resource://com.ruidiculous.mywebviewdemo/"+R.raw.gta6; private VideoView videoView; private Button btnLoop , btnVoiceSilence, btnVoiceResume; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_d_k_video_player); VideoViewManager.setConfig(VideoViewConfig.newBuilder() //使用使用IjkPlayer解码 .setPlayerFactory(IjkPlayerFactory.create()) .build()); videoView = findViewById(R.id.videoView); videoView.setUrl(uri); //设置视频地址 // 视频界面:封面,全屏,底部控制条,播放暂停等等 StandardVideoController controller = new StandardVideoController(this); controller.addDefaultControlComponent("标题", false); videoView.setVideoController(controller); //设置控制器 videoView.addOnStateChangeListener(this); videoView.start(); //开始播放,不调用则不自动播放 btnLoop = findViewById(R.id.btnLoop); btnVoiceSilence = findViewById(R.id.btnVoiceSilence); btnVoiceResume = findViewById(R.id.btnVoiceResume); btnLoop.setOnClickListener(this); } @Override public void onClick(View v) { switch ( v.getId() ){ case R.id.btnLoop: videoView.setLooping(true); break; case R.id.btnVoiceSilence: videoView.setMute(true); break; case R.id.btnVoiceResume: videoView.setMute(false); break; } } @Override public void onPlayerStateChanged(int playerState) { } @Override public void onPlayStateChanged(int playState) { switch (playState) { case VideoView.STATE_IDLE: break; case VideoView.STATE_PREPARING: //准备中 Toast.makeText(this,"准备中",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_PREPARED: //准备完成 Toast.makeText(this,"准备完成",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_PLAYING: //播放中 //需在此时获取视频宽高 int[] videoSize = videoView.getVideoSize(); L.d("视频宽:" + videoSize[0]); L.d("视频高:" + videoSize[1]); break; case VideoView.STATE_PAUSED: //暂停 Toast.makeText(this,"暂停",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_BUFFERING: break; case VideoView.STATE_BUFFERED: break; case VideoView.STATE_PLAYBACK_COMPLETED: //播放完成 Toast.makeText(this,"播放完成",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_ERROR: break; } } }
2.功能:
重复播放:
videoView.setLooping(true);
声音静音:
videoView.setMute(true);
取消全屏:
case xyz.doikki.videoplayer.player.VideoView.PLAYER_FULL_SCREEN://全屏 videoView.setScreenScaleType(xyz.doikki.videoplayer.player.VideoView.SCREEN_SCALE_DEFAULT); videoView.stopFullScreen(); break;
暂停播放:
videoView.pause();
开始播放:
videoView.start();
释放资源:
videoView.release();
倍速播放:
mVideoView.setSpeed(1.0f);
视频截图:
ImageView imageView = findViewById(R.id.iv_screen_shot); Bitmap bitmap = mVideoView.doScreenShot(); imageView.setImageBitmap(bitmap);
设置基本控制器:
创建控制器对象,调用控制器对象.addDefaultControlComponent("标题", false)为控制器进行基础设置
StandardVideoController controller = new StandardVideoController(this); controller.addDefaultControlComponent("标题", false); //控制器进行默认设置 videoView.setVideoController(controller); //设置控制器
视频控制器具体:
通过添加多类别界面组合整个视频播放UI界面,分为:自动完成播放界面,错误界面,标题界面,点播控制条界面。
这里不用默认,自行添加需要的界面。
//视频界面:封面,全屏,底部控制条,播放暂停等等 StandardVideoController controller = new StandardVideoController(this); //controller.addDefaultControlComponent("标题", false); controller.addControlComponent(new CompleteView(this));//自动完成播放界面 controller.addControlComponent(new ErrorView(this));//错误界面 TitleView titleView = new TitleView(this);//标题栏 titleView.setTitle("标题!"); controller.addControlComponent(titleView); //根据是否为直播设置不同的底部控制条 VodControlView vodControlView = new VodControlView(this);//点播控制条 MyVodControlView myVodControlView = new MyVodControlView(this); controller.addControlComponent(vodControlView); GestureView gestureControlView = new GestureView(this);//滑动控制视图 controller.addControlComponent(gestureControlView); //根据是否为直播决定是否需要滑动调节进度 controller.setCanChangePosition(true); videoView.setVideoController(controller); //设置控制器 videoView.addOnStateChangeListener(this); videoView.start(); //开始播放,不调用则不自动播放 // 视频已实现暂停,继续,拖动
2. 修改或扩展功能
比如要修改点播控制条,去除全屏功能。
1. 复制VodControlView代码
2. 对内部组件进行修改比如 mFullxxx.setVisiable(GONE);
3. 调用controller.addControlComponent(myVodControlView); 添加自定义视图
完整项目代码
public class DKVideoPlayerActivity extends AppCompatActivity implements View.OnClickListener ,VideoView.OnStateChangeListener { private String uri = "android.resource://com.ruidiculous.mywebviewdemo/"+R.raw.gta6; private VideoView videoView;private Button btnLoop , btnVoiceSilence, btnVoiceResume; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_d_k_video_player); VideoViewManager.setConfig(VideoViewConfig.newBuilder() //使用使用IjkPlayer解码 .setPlayerFactory(IjkPlayerFactory.create()) .build()); videoView = findViewById(R.id.videoView); videoView.setUrl(uri); //设置视频地址 //视频界面:封面,全屏,底部控制条,播放暂停等等 StandardVideoController controller = new StandardVideoController(this); //controller.addDefaultControlComponent("标题", false); controller.addControlComponent(new CompleteView(this));//自动完成播放界面 controller.addControlComponent(new ErrorView(this));//错误界面 TitleView titleView = new TitleView(this);//标题栏 titleView.setTitle("标题!"); controller.addControlComponent(titleView); //根据是否为直播设置不同的底部控制条 VodControlView vodControlView = new VodControlView(this);//点播控制条 MyVodControlView myVodControlView = new MyVodControlView(this); controller.addControlComponent(vodControlView); GestureView gestureControlView = new GestureView(this);//滑动控制视图 controller.addControlComponent(gestureControlView); //根据是否为直播决定是否需要滑动调节进度 controller.setCanChangePosition(true); videoView.setVideoController(controller); //设置控制器 videoView.addOnStateChangeListener(this); videoView.start(); //开始播放,不调用则不自动播放 // 视频已实现暂停,继续,拖动 btnLoop = findViewById(R.id.btnLoop); btnVoiceSilence = findViewById(R.id.btnVoiceSilence); btnVoiceResume = findViewById(R.id.btnVoiceResume); btnLoop.setOnClickListener(this); } @Override public void onClick(View v) { switch ( v.getId() ){ case R.id.btnLoop: videoView.setLooping(true); break; case R.id.btnVoiceSilence: videoView.setMute(true); break; case R.id.btnVoiceResume: videoView.setMute(false); break; } } @Override public void onPlayerStateChanged(int playerState) { }
@Override public void onPlayStateChanged(int playState) { switch (playState) { case VideoView.STATE_IDLE: break; case VideoView.STATE_PREPARING: //准备中 Toast.makeText(this,"准备中",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_PREPARED: //准备完成 Toast.makeText(this,"准备完成",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_PLAYING: //播放中 //需在此时获取视频宽高 int[] videoSize = videoView.getVideoSize(); L.d("视频宽:" + videoSize[0]); L.d("视频高:" + videoSize[1]); break; case VideoView.STATE_PAUSED: //暂停 Toast.makeText(this,"暂停",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_BUFFERING: break; case VideoView.STATE_BUFFERED: break; case VideoView.STATE_PLAYBACK_COMPLETED: //播放完成 Toast.makeText(this,"播放完成",Toast.LENGTH_SHORT).show(); break; case VideoView.STATE_ERROR: break; } } }