視頻播放,有幾中方式:java
// 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);
複製代碼
VideoView,實際上是集成了 MediaPlayer 和 SurfaceView 的一個控件。android
// 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 */
複製代碼
// 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();
}
}
複製代碼