安卓中的视频播放器
安卓中我们基本上会用到三种方式来播放视频
第一种,系统的视频播放器:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击调取系统的播放器
public void getdata(View v){
//Intent.ACTION_VIEW 系统播放器 打开视频播放器
Intent it = new Intent(Intent.ACTION_VIEW);
//加载路径资源 类型
Uri data = Uri.parse(Environment.getExternalStorageDirectory()+"/minion_08.mp4");
it.setDataAndType(data, "video/*");
startActivity(it);
/**
*act=android.intent.action.VIEW
*dat=/storage/sdcard/minion_08.mp4 typ=video/*
*cmp=com.android.gallery/com.android.camera.MovieView
*/
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击调取系统的播放器
public void getdata(View v){
//Intent.ACTION_VIEW 系统播放器 打开视频播放器
Intent it = new Intent(Intent.ACTION_VIEW);
//加载路径资源 类型
Uri data = Uri.parse(Environment.getExternalStorageDirectory()+"/minion_08.mp4");
it.setDataAndType(data, "video/*");
startActivity(it);
/**
*act=android.intent.action.VIEW
*dat=/storage/sdcard/minion_08.mp4 typ=video/*
*cmp=com.android.gallery/com.android.camera.MovieView
*/
}
}
第二种,VideoView播放视频
public class MainActivity extends Activity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
videoView = (VideoView) findViewById(R.id.video);
//加载视频
videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/minion_08.mp4");
//得到媒体控制器
MediaController mediaController = new MediaController(this);
//设置媒体控制器
videoView.setMediaController(mediaController);
//对媒体控制器设置控制的视图 锚
mediaController.setAnchorView(videoView);
//播放
videoView.start();
}
}
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
videoView = (VideoView) findViewById(R.id.video);
//加载视频
videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/minion_08.mp4");
//得到媒体控制器
MediaController mediaController = new MediaController(this);
//设置媒体控制器
videoView.setMediaController(mediaController);
//对媒体控制器设置控制的视图 锚
mediaController.setAnchorView(videoView);
//播放
videoView.start();
}
}
布局文件代码是:
<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"
tools:context="${relativePackage}.${activityClass}" >
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
第三种,SurfaceView播放视频
public class MainActivity extends Activity {
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件ID
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
//加载资源
mediaPlayer = MediaPlayer.create(this,Uri.parse(Environment.getExternalStorageDirectory()+"/minion_08.mp4"));
new Thread(){
public void run() {
SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(new Callback() {
//视图销毁
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
//视图创建
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//进行播放视频
//设置视图
mediaPlayer.setDisplay(holder);
mediaPlayer.start();
}
//改变
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
};
}.start();
}
}
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件ID
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
//加载资源
mediaPlayer = MediaPlayer.create(this,Uri.parse(Environment.getExternalStorageDirectory()+"/minion_08.mp4"));
new Thread(){
public void run() {
SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(new Callback() {
//视图销毁
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
//视图创建
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//进行播放视频
//设置视图
mediaPlayer.setDisplay(holder);
mediaPlayer.start();
}
//改变
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
};
}.start();
}
}
布局文件代码是:
<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"
tools:context="${relativePackage}.${activityClass}" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>