VideoView
使用VideoView步驟
通過VideoView播放視頻的步驟:
- 在界面佈局文件中定義VideoView組件,或在程序中創建VideoView組件
- 調用VideoView的如下兩個方法來加載指定的視頻
setVidePath(String path):加載path文件代表的視頻
setVideoURI(Uri uri):加載uri所對應的視頻 - 調用VideoView的start()、stop()、psuse()方法來控制視頻的播放
VideoView通過與MediaController類結合使用,開發者可以不用自己控制播放與暫停
簡單的VideoView實例
加權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
佈局
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放視頻"/>
<VideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
活動
public class MainActivity extends Activity implements View.OnClickListener{
@ViewInject(R.id.btn_video_view)
private Button mBtnViewVideo;
@ViewInject(R.id.videoview)
private VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
}
@OnClick({R.id.btn_video_view})
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_video_view:
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/DCIM/100MEDIA/VIDEO0001.mp4");
mVideoView.setMediaController(new MediaController(MainActivity.this));
mVideoView.start();
break;
default:
break;
}
}
}