有三种方法播放视频文件。
1.使用系统自带的播放器
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory()
.getPath()+"/test.mp4");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}
}
2.使用surfaceView
public class MainActivity extends Activity implements SurfaceHolder.Callback,
OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener {
SurfaceView surfaceView;
SurfaceHolder holder;
String path;
MediaPlayer mediaPlayer;
int videoWidth;
int videoHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surface);
holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepared(MediaPlayer arg0) {
videoWidth = mediaPlayer.getVideoWidth();
videoHeight = mediaPlayer.getVideoHeight();
if (videoWidth != 0 && videoHeight != 0) {
holder.setFixedSize(videoWidth, videoHeight);// 设置视频的宽度和高度
mediaPlayer.start();
}
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
@Override
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
path = Environment.getExternalStorageDirectory().getPath()+"/test.mp4";
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(path);
mediaPlayer.setDisplay(holder);// 设置通过surfaceView来显示画面
mediaPlayer.prepare();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.videoplayerwithsurfaceview.MainActivity" >
<SurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
3.使用VideoView
public class MainActivity extends Activity {
VideoView videoView;
Button play,pause,load;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video);
play = (Button) findViewById(R.id.btn_play);
pause = (Button) findViewById(R.id.btn_pause);
load = (Button) findViewById(R.id.btn_load);
load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
videoView.setVideoPath(Environment.getExternalStorageDirectory().getPath()+"/test.mp4");
videoView.setMediaController(new MediaController(MainActivity.this));
videoView.requestFocus();
}
});
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
videoView.start();
}
});
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
videoView.pause();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/video"
android:layout_width="fill_parent"
android:layout_height="200dp" />
<Button
android:id="@+id/btn_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/video"
android:layout_marginLeft="50dp"
android:text="装载" />
<Button
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/video"
android:layout_toRightOf="@id/btn_load"
android:text="暂停" />
<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/video"
android:layout_toRightOf="@id/btn_pause"
android:text="播放" />
</RelativeLayout>