1.需要在activity中判断
if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
return;
2.缓冲状态的回掉,用于显示加载进度(其实是设置MediaPlayer的该方法)
mVideoView.setOnBufferingUpdateListener(new OnBufferingUpdateListener() { @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { // 缓冲状态中,percent为下载量的百分比 } });
3.不管同步异步调用mMediaPlayer.prepare,都会回掉该监听器的方法
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.setPlaybackSpeed(1.0f); } });4.播放结束将会回调,前提是没有开启循环模式
mWebView.setOnCompletionListener(mCompletionListener);5.播放过程中出现错误将会回掉
mVideoView.setOnErrorListener(mErrorListener);
6.seekTo(int)方法是异步执行的,所以它可以马上返回,但是实际的定位播放操作可能需要一段时间才能完成,尤其是播放流形式的音频/视频。当实际的定位播放操作完成之后,内部的播放引擎会调用客户端程序员提供的OnSeekComplete.onSeekComplete()回调方法。
mVideoView.setOnSeekCompleteListener(mSeekCompleteListener);7.回掉播放过程中的各种信息
mVideoView.setOnInfoListener(mInfoListener);
以下是系统MediaPlayer的静态字段
MEDIA_INFO_UNKNOWN:未知消息 MEDIA_INFO_VIDEO_TRACK_LAGGING:视频解码器太复杂了:它不能解码帧的足够快。可能只有音频播放良好在这个阶段。 MEDIA_INFO_VIDEO_RENDERING_START:只是渲染了视频的第一帧。 MEDIA_INFO_BUFFERING_START:暂停播放以缓存更多地数据。 MEDIA_INFO_BUFFERING_END:缓冲结束,将要继续进入播放状态。 MEDIA_INFO_BAD_INTERLEAVING: MEDIA_INFO_NOT_SEEKABLE:不能拖动 MEDIA_INFO_METADATA_UPDATE: MEDIA_INFO_UNSUPPORTED_SUBTITLE:该媒体框架不支持该字幕 MEDIA_INFO_SUBTITLE_TIMED_OUT:字幕读取时间过长
MEDIA_INFO_DOWNLOAD_RATE_CHANGED:下载的速率发生了改变(单位kb/s)
MediaController mediaController = new MediaController(TvPlayActivity.this); // 全屏半屏按钮点击事件 mediaController.setIvToBigClickListener(new OnClickListener() { @Override public void onClick(View v) { // 设置屏幕强转 if (TvPlayActivity.this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { TvPlayActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else if (TvPlayActivity.this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { TvPlayActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } });MediaController中一般按钮的监听里面已经设置好,比如开始播放,暂停,seekbar拖动,然后以上方法可以设置横竖屏切换的监听。
// 设置uri if (!TextUtils.isEmpty(videoInfoVo.getProgramlist().get(0).getM3u8())) { // 设置控制器 MediaController mediaController = new MediaController(TvPlayActivity.this); // 全屏半屏按钮点击事件 mediaController.setIvToBigClickListener(new OnClickListener() { @Override public void onClick(View v) { // 设置屏幕强转 if (TvPlayActivity.this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { TvPlayActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else if (TvPlayActivity.this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { TvPlayActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } }); mVideoView.setMediaController(mediaController); // 获得焦点 mVideoView.requestFocus(); // 设置监听器 mVideoView.setOnBufferingUpdateListener(new OnBufferingUpdateListener() { @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { // 缓冲状态中,percent为下载量的百分比,一般在videoView中间设置 } }); mVideoView.setOnInfoListener(new MediaInfoListener()); mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.setPlaybackSpeed(1.0f); } }); Uri uri = Uri.parse(videoInfoVo.getProgramlist().get(0).getM3u8()); mVideoView.setVideoURI(uri); }
class MediaInfoListener implements OnInfoListener { @Override public boolean onInfo(MediaPlayer arg0, int arg1, int arg2) { switch (arg1) { case MediaPlayer.MEDIA_INFO_BUFFERING_START: // 开始缓冲,如果正在播放,则停止 if (mVideoView.isPlaying()) { mVideoView.pause(); } break; case MediaPlayer.MEDIA_INFO_BUFFERING_END: // 缓冲完毕,开始播放 mVideoView.start(); break; case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED: // 下载的速率发生了改变 // setText("" + arg2 + "kb/s" + " "); break; } return true; } }当点击横竖屏时,需要手动设置控件的宽高:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { mTopRl.getLayoutParams().height = LayoutParams.MATCH_PARENT; mTopRl.getLayoutParams().width = LayoutParams.MATCH_PARENT; mVideoView.getLayoutParams().height = LayoutParams.MATCH_PARENT; mVideoView.getLayoutParams().width = LayoutParams.MATCH_PARENT; // mVideoView.getHolder().setFixedSize(mVideoView.getLayoutParams().width, mVideoView.getLayoutParams().height); // mVideoView.setVideoScale(Settings.DISPLAY_HEIGHT, Settings.DISPLAY_WIDTH); // mVideoView.setVideoScale(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // mVideoView.getHolder().setFixedSize(Settings.DISPLAY_HEIGHT, Settings.DISPLAY_WIDTH); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { // mVideoView.getLayoutParams().height= DensityUtils.dp2px(TvPlayActivity.this, 230); // mVideoView.getLayoutParams().width= LayoutParams.MATCH_PARENT; mTopRl.getLayoutParams().height = LayoutParams.WRAP_CONTENT; mTopRl.getLayoutParams().width = LayoutParams.MATCH_PARENT; mVideoView.setVideoScale(LayoutParams.MATCH_PARENT, DensityUtils.dp2px(TvPlayActivity.this, 230)); mVideoView.getHolder().setFixedSize(mVideoView.getLayoutParams().width, mVideoView.getLayoutParams().height); } }