上一节我给大家介绍了一种使用“SurfaceView + MediaPlayer”播放视频的方式,那有没有一种Android控件,既包含SurfaceView又包含MediaPlayer呢?当然是有的了那就是这次我要给大家介绍的控件VideoView。
简介
VideoView功能就是播放视频,该类可以加载各种来源的图像,并且可以计算视频的尺寸所以它可以使用在各种布局管理器中,并且提供各种显示操作例如缩放、着色。
但需要注意的是:VideoView进入后台的时候不会保存它自身的完整状态,特别不会保存当前播放状态、播放位置、所选曲目、和任何通过addSubtitleSource()添加的曲目。应用程序需要通过onSaveInstanceState(Bundle)进行保存,通过onRestoreInstanceState(Bundle)进行恢复。
还需注意在音频会话中,从getaudiosessionid()的值,可能会被VideoView在上次恢复时候修改。
VideoView 继承了类SurfaceView,实现了接口MediaController.MediaPlayerControl。从继承关系来看,VideoView就是一个增强的SurfaceView,并且具备媒体播放器的控制功能。那是否还缺少一个MediaPlayer对象呢?是的MediaPlayer
是它内部的一个域。
实现
Java部分
实现的非常简洁
public class VideoViewActivity extends AppCompatActivity {
VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
mVideoView = (VideoView)this.findViewById(R.id.video_view);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_start:
mVideoView.start();
Uri uri = Uri.parse("/mnt/sdcard/qq.mp4");
mVideoView.setVideoURI(uri);
break;
case R.id.play_pause:
if(mVideoView.isPlaying()){
mVideoView.pause();
}else{
mVideoView.start();
}
break;
case R.id.play_stop:
if(mVideoView.isPlaying()){
mVideoView.suspend();
}
break;
case R.id.test:
startActivity(new Intent(this, TestActivity.class));
break;
}
}
}
Layout部分
<?xml version="1.0" encoding="utf-8"?>
<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="club.anhe.surfaceviewdemo.surfaceviewdemo.VideoViewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/voide_layout"
android:background="@android:color/black"
android:orientation="vertical">
<VideoView
android:id="@+id/video_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="480px">
</VideoView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_below="@id/voide_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button
android:text="播放"
android:id="@+id/play_start"
android:onClick="onClick"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="暂停"
android:id="@+id/play_pause"
android:onClick="onClick"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="停止"
android:id="@+id/play_stop"
android:onClick="onClick"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="测试"
android:id="@+id/test"
android:onClick="onClick"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</RelativeLayout>
下载路径
https://github.com/kingroc711/AndroidMediaPlayer/releases/tag/VideoView