標籤:
VideoView實際上是android SDK中封裝好的一個用於播放視頻的組件,使用步驟如下:
1.在布局檔案中定義Video組件;
2.使用以下方法載入指定視頻:
1)setVideoPath(String path)
2)setVideoURI(Uri uri)
3.調用Video的start()、stop()、pause()方法來控制視頻播放。
例子如下:
布局檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><!-- 定義VideoView播放視頻 --><VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
範例代碼,使用以下代碼可以實現播放視頻
videoView = (VideoView) findViewById(R.id.video);File video = new File("/mnt/sdcard/test.mp4");if(video.exists()){ videoView.setVideoPath(video.getAbsolutePath()); videoView.start();}
如果想實現一個有控制條的視頻播放效果,可以使用以下代碼
videoView = (VideoView) findViewById(R.id.video);mController = new MediaController(this);File video = new File("/mnt/sdcard/test.mp4");if(video.exists()){ videoView.setVideoPath(video.getAbsolutePath());
// 設定videoView與mController建立關聯 videoView.setMediaController(mController);
// 設定mController與videoView建立關聯 mController.setMediaPlayer(videoView);
}
效果如:
android開發學習之音視頻-VideoView