1、有关Vitamio的作品有很多,最后发现找了很多都不是自己想要,倒不如自己写一个,毕竟公司要求的不高;
2、要求:固定视频的大小;实现mediaController依附在videoView中;可以播放URL链接的视频,可以进度条自由移动;
最后的效果图:
开始做吧:
为什么要自定义呢,如果用自带会出现不会依附在VideoView 中,而且控制器会全屏的播放,就像下图一样:
1.1贴的是布局的文件,由于我的视频是要放在右上角,布局如下;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1.6" android:orientation="horizontal" > <TextView android:id="@+id/subtitle_view" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2" android:text="干啥子呢,这个地方大家不用管" /> <RelativeLayout android:id="@+id/videoViewRel" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1.8" android:background="@android:color/white"> <ImageView android:id="@+id/showVideoImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@null" android:src="@null"/> <io.vov.vitamio.widget.VideoView android:id="@+id/surface_view" android:layout_width="794dp" android:layout_height="540dp" /> <LinearLayout android:id="@+id/media_control_linear" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:gravity="center" android:orientation="horizontal" android:background="#e0000000" > <ImageButton android:id="@+id/start" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="5dp" android:padding="2dp" android:src="@drawable/iconfont_bofang"/> <TextView android:id="@+id/load_rate" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center_vertical" android:padding="5dp" android:text="00:00" /> <SeekBar android:id="@+id/mediacontroller_progress" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:max="100" android:maxHeight="5dp" android:minHeight="5dp" android:progress="0" android:secondaryProgress="0" > </SeekBar> <TextView android:id="@+id/load_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="5dp" android:text="00:00" /> </LinearLayout> </RelativeLayout> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="2" android:text="干啥子呢,这个也要办过来" /> </LinearLayout>
1.2贴主要代码:
1.2.1 初始化VideoView数据
/** * * 初始化初始化界面之后的数据 * @date 2015年9月5日 * @author liuyonghong */ private void initVideoViewData() { if (subtitle_path == "") { Toast.makeText(VideoViewSubtitle.this, "数据链接有误", Toast.LENGTH_LONG).show(); return; } else { //确保视频播放的质量 mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_MEDIUM); mVideoView.setVideoChroma(MediaPlayer.VIDEOCHROMA_RGB565);; mVideoView.setMediaController(new MediaController(this)); setInstantSeeking(true); //设置视频播放的缩列图 Bitmap mBitmap=ThumbnailUtils.createVideoThumbnail(mContext, subtitle_path, Video.Thumbnails.MINI_KIND); showVideoImage.setImageBitmap(mBitmap); //这个方法是视频准备完全后的接口 mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { //视频播放的速度 mediaPlayer.setPlaybackSpeed(1.0f); //挂载外面的字幕 mVideoView.addTimedTextSource(subtitle_path); mVideoView.setTimedTextShown(true); } }); //视频播放结束后的动作 mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { setBeginView(); isFirststart=true; mHandler.removeMessages(SHOW_PROGRESS); mHandler.removeMessages(FADE_OUT); } }); //这个很重要,是用来监听videoView被触摸以后的效果 mVideoView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i(TAG, "setOnTouchListener.onTouch"); show(sDefaultTimeout); return false; } }); } doPauseResume(); mVideoView.setMediaController(new MediaController(this)); }
1.2.2贴有关点击了播放按钮前,在视频播放完以后的状态
/** *void 初始化数据和界面 * @date 2015年9月6日 * @author liuyonghong */ private void setBeginView() { mVideoView.pause();//视频停止播放 showVideoImage.setVisibility(View.VISIBLE);//缩列图的可见 mVideoView.setVisibility(View.GONE);//视频控件不可见 media_control_linear.setVisibility(View.VISIBLE);//medioControl可见 mProgress.setProgress(0);//进度条不可见 mCurrentTime.setText("00:00");//时间归零 start.setImageResource(R.drawable.iconfont_bofang);//调回播放按钮 }
1.2.3 有关控制medioControl控制界面的显示和隐藏的Handler
//有关控制时间的显示MedioControl见面的handler
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
long pos;
switch (msg.what) {
case FADE_OUT:
hide();
break;
case ISFIRST:
isFirststart=false;
break;
case SHOW_PROGRESS:
Log.i(TAG, "SHOW_PROGRESS");
pos = setProgress();
if (!mDragging && mShowing) {
msg = obtainMessage(SHOW_PROGRESS);
sendMessageDelayed(msg, 1000 - (pos % 1000));
updatePausePlay();
}
break;
}
}
};
主要事项:
如果你要转移的话一定要注意
//下面这件话很重要,一定要加,另外在权限文件中一定要加android:name="io.vov.vitamio.activity.InitActivity";
if (!LibsChecker.checkVitamioLibs(this)) return;
其他的源代码请查看有关上传的源代码和Vitamio的官网;
代码是又开源的代码删除了其他的只保留VideoViewSubtitle.java 界面,功能不复杂;
如果公司未来说要加强这个视频播放器的功能,我再写博客完善;
如有写的代码不好的地方,出错的地方请大家指正;
刚开始写博客,不知道怎么上传代码,就上传到了百度网盘:
链接:http://pan.baidu.com/s/1sj3MB3f 密码:wsts