概述
视频播放,有几中方式:
- 调用android自带的视频播放器
- 使用VideoView
- 使用SurfaceView结合MediaPlayer
- 第三方开源库
1. 调用android本地的视频播放器
// 0. 定义好视频的路径
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/hehe.mp4");
// 1. 先设定好Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
// 2. 设置好 Data:播放源,是一个URI
// 设置好 Data的Type:类型是 “video/mp4"
intent.setDataAndType(uri,"video/*");
// 3. 跳转:
startActivity(intent);
2. 使用VideoView
VideoView,其实是集成了 MediaPlayer 和 SurfaceView 的一个控件。
// 1. 新建 VideoView 的对象
private VideoView videoView = null;
// 2. 设置全屏,以防万一
/* 设置播放视频时候不需要的部分 *//* 以下代码需要写在setContentView();之前 */
requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉 title
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置全屏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //设置屏幕常亮
// 3. 获取组件对象
videoView = (VideoView) findViewById(R.id.videoView);
// 4. videoview 的设置
// 4.1 获取MediaController对象,控制媒体播放,这里应该是获取 android.widget.MediaController 的对象
android.widget.MediaController mediaController = new android.widget.MediaController(this);
// 4.2 绑定到 Video View
videoView.setMediaController(mediaController);
// 4.3 设置 URI,播放源
//Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/hehe2.mp4");
//videoView.setVideoURI(uri);
videoView.setVideoPath(Environment.getExternalStorageDirectory().getPath() + "/hehe2.mp4");
// 4.4 开始播放
videoView.start();
// 4.5 获取焦点
videoView.requestFocus();
/**
* 读取本地文件需要权限:
* <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
参考:
http://blog.csdn.net/x605940745/article/details/18263729
http://my.oschina.net/mastere/blog/164628
*/
3. MediaPlayer 结合 SurfaceView
// 1. 新建所需要的对象
private SurfaceView surfaceView = null;
private MediaPlayer mediaPlayer = null;
// 2. 绑定控件
surfaceView = (SurfaceView) findViewById(R.id.SurfaceView);
mediaPlayer = new MediaPlayer();
// 3. 设置 SurfaceView
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
play();
Log.e("flag","surfaceCreated");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.e("flag","surfaceChanged");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("flag","surfaceDestroyed");
}});
/**
* 播放,暂停,停止方法。
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_play :
if (!mediaPlayer.isPlaying()) {
play();
}
break;
case R.id.button_pause : pause(); break;
case R.id.button_stop : stop(); break;
}
}
private void stop() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
private void pause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
else {
mediaPlayer.start();
}
}
private void play() {
try {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置需要播放的视频
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/hehe.mp4");
//把视频画面输出到SurfaceView
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.prepare();
//播放
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}