VideoView 是Android 自帶的可以播放視頻的控件。
它的使用方法也比較簡單,相當于是surfaceview+mediaplayer 兩者的集合體。
對于一個控件的使用 無外乎 三步:
1. 初始化
2 設置數據、添加監聽事件的方法
3.在監聽方法中 進行處理事件。
對于VideoView來說也不例外, 首先我們可以再xml 布局中引用videoview。
~~~
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"/>
</RelativeLayout>
~~~
接下來在相關界面進行初始化設置:
~~~
/**
* 準備播放工作
*/
private void initVideoView() {
String uri = ConfigManage.URL_SPLASH;
videoView.setVideoURI(Uri.parse(uri));
videoView.setOnPreparedListener(this);
// 設置全屏
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
videoView.setLayoutParams(layoutParams);
videoView.setOnCompletionListener(this);
}
~~~
這里設置了 onPrepareListener() 與 OnCompletionListener() 因此我們實現這兩個方法
~~~
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.start();
videoView.requestFocus();
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
goToMainActivity();
}
~~~
下面是具體效果,還不錯吧,播放起來還是很流暢的,

但是Video播放視頻時有格式限制的,應該是僅僅只有 3gp與 MP4 可用, 播放網絡視頻需要添加聯網權限。
完整代碼沒有做成Demo,需要的可以自己去摘,在項目中的SplashActivity:[https://github.com/CodingForAndroid/appone/](https://github.com/CodingForAndroid/appone/)