直接在主Activity中通過動態(tài)創(chuàng)建ViewPlayer的方式來實現(xiàn)谣沸,app啟動時的啟動視頻播放玷过。
- 直接代碼完事
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 創(chuàng)建VideoView實例
videoView = new VideoView(this);
String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.your_video_file;
Uri videoUri = Uri.parse(videoPath);
this.videoView.setVideoURI(videoUri);
this.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// 視頻播放完畢苛蒲,移除VideoView
((ViewGroup) videoView.getParent()).removeView(videoView);
}
});
this.addContentView(videoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
this.videoView.start();
}
}
- 上面的
your_video_file
需要放在res/raw/your_video_file.mp4下卤橄。 可以改名為自己定義的視頻名
完事。