系统自带的VideoView有些视频格式不支持,那么我们可以用第三方实现的VideoView替代系统的来播放视频,比较流行的有ijkplayer、vitamio。
最近有个需求就是需要给视频添加字幕,其实也挺简单的。字幕比较常用的格式是srt,实际它就是文本,把它解析出来,然后根据时间再展示就OK。还有就是实现了即使旋转按钮关闭,根据方向感应器也能做到横竖屏切换。
本文用的是系统VideoView,然后播放sd卡中的视频来作为演示(源码中带有f2.mp4和f2.srt,运行时拷贝到sd卡就行)。下面简单介绍一下源码:
MainActivity包括显示字幕和如何实现横竖屏如何切换:
public class SubtitleActivity extends Activity implements View.OnClickListener,OnTouchListener{
private VideoView videoView ;
TextView tvSrt, mCurrentTime,mTotalTime,resolution_switch,mediacontroller_file_name;
ImageView mediacontroller_play_pause,switch_screen;
private SeekBar progress_seekbar;
private AudioManager mAM;
private long totalDuration;
private boolean mShowing = true, mDragging,isResolution;
private static final int PARSE_SRT = 0;
private static final int FADE_OUT = 1;
private static final int SHOW_PROGRESS = 2;
private static final int CHANGE_VIDEOVIEW_BG = 3;
private static final int SCREEN_ORIENTATION_USER = 4;
private static final int sDefaultTimeout = 3000;
private RelativeLayout videoview_layout, mMediaController;
private ListView resolution_listview;
private boolean isPortraint = true;
private static int LockScreen = -1;// 用于记录是否关闭屏幕旋转,0为关闭1为开启
private int screenWidth,videoViewHeight;
List videopathList=new ArrayList();
Handler mHandler=new Handler(){
public void handleMessage(Message msg){
long pos;
switch (msg.what) {
case PARSE_SRT:
SrtParser.showSRT(videoView,tvSrt) ;
//每隔500ms执行一次showSRT(),根据时间匹配显示哪句字幕
mHandler.sendEmptyMessageDelayed(0, 500);
break;
case FADE_OUT:
showOrHideController();
break;
case SHOW_PROGRESS:
pos = setControllerProgress();
if (!mDragging && mShowing) {
msg = obtainMessage(SHOW_PROGRESS);
sendMessageDelayed(msg, 1000 - (pos % 1000));
}
break;
case CHANGE_VIDEOVIEW_BG:
videoView.setBackgroundColor(0x00000000);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subtitle);
videoView = (VideoView)this.findViewById(R.id.videoView );
mAM = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
screenWidth = APPApplication.screenWidth;
videoViewHeight = screenWidth * 9 / 16;
tvSrt = (TextView)findViewById(R.id.srt);//项目中显示字幕的控件
mediacontroller_file_name= (TextView)findViewById(R.id.mediacontroller_file_name);
//String[]splitStr=Constant.videoUrl1.split("/");
//mediacontroller_file_name.setText(splitStr[splitStr.length-1]);
mTotalTime = (TextView) findViewById(R.id.mediacontroller_time_total);
mCurrentTime = (TextView) findViewById(R.id.mediacontroller_time_current);
resolution_switch = (TextView) findViewById(R.id.resolution_switch);
mediacontroller_play_pause = (ImageView) findViewById(R.id.mediacontroller_play_pause);
switch_screen = (ImageView) findViewById(R.id.switch_screen);
videoview_layout = (RelativeLayout) findViewById(R.id.videoview_layout);
mediacontroller_play_pause.setOnClickListener(this);
progress_seekbar = (SeekBar) findViewById(R.id.mediacontroller_seekbar);
videoview_layout = (RelativeLayout) findViewById(R.id.videoview_layout);
mMediaController = (RelativeLayout) findViewById(R.id.media_controller);
resolution_listview = (ListView) findViewById(R.id.resolution_listview);
resolution_switch.setOnClickListener(this);
videoView.setOnTouchListener(this);
progres