由于公司项目需要自己写播放器,于是用VideoView写了个,废话不多说,直接进入主题。 首先介绍播放器的主界面
以上是播放器的主界面,主要包括 自定义视频播放界面 com.example.vediotest.FullScreenVideoView 播放暂停按钮 、 当前播放进度、视频总时间、进度条、手势改变音量和亮度大小、全屏切换、载入视频及缓冲提示loading.
下面介绍 视频播放代码
Uri uri = Uri.parse("http://ignhdvod-f.akamaihd.net/i/assets.ign.com/videos/zencoder/,"
+ "416/d4ff0368b5e4a24aee0dab7703d4123a-110000,640/d4ff0368b5e4a24aee0dab7703d4123a-500000,"
+ "640/d4ff0368b5e4a24aee0dab7703d4123a-1000000,960/d4ff0368b5e4a24aee0dab7703d4123a-2500000,"
+ "1280/d4ff0368b5e4a24aee0dab7703d4123a-3000000,-1354660143-w.mp4.csmil/master.m3u8");
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
另外不得不说的是视频播放器VideoView的几个监听类
mVideoView.setOnErrorListener(this);//视频播放出现错误时
mVideoView.setOnPreparedListener(this);//视频准备监听类
/**
* 视频播放完成
*/
mVideoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.i("123", "onCompletion");
}
});
/**
* 视频播放过程中信息,比如出现缓冲状态,我们可以在这里进行监听
*/
mVideoView.setOnInfoListener(new OnInfoListener() {
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
mProgressBar.setVisibility(View.VISIBLE);
Log.i("123", "缓冲中...." + mVideoView.getBufferPercentage());
} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
// 此接口每次回调完START就回调END,若不加上判断就会出现缓冲图标一闪一闪的卡顿现象
if (mp.isPlaying()) {
mProgressBar.setVisibility(View.GONE);
Log.i("123",
"缓冲完成..." + mVideoView.getBufferPercentage());
}
}
return true;
}
});
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.i("123", "播放出错");
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer = mp;
Log.i("123", "onPrepared");
mProgressBar.setVisibility(View.GONE);
mVideoView.start();
mStart.setBackgroundResource(R.drawable.pause);
}
在进行全屏切换过程中的时候 我们在使用onConfigurationChanged ,看到网上有在OnStart()和onResume()总切换,试了下 效果不好,下面附上代码
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
/**
* 改变窗口,去掉Title
*/
WindowManager.LayoutParams mParams = getWindow().getAttributes();
mParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(mParams);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
mRelativeLayout.setLayoutParams(params);//重新定义播放器界面大小
} else {
WindowManager.LayoutParams mParams = getWindow().getAttributes();
mParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(mParams);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, 400);
mRelativeLayout.setLayoutParams(params);
}
super.onConfigurationChanged(newConfig);
}
添加android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
如下
如不加
onConfigurationChanged 方法不能触发
以上就是播放器的原理及部分代码,本人不善言辞,如有错误,请帮忙指正批评,谢谢!
源码下载请移步 播放器源码
本文属于原创,若转载 请说明