直接在主Activity中通过动态创建ViewPlayer的方式来实现,app启动时的启动视频播放。
- 直接代码完事
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建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下。 可以改名为自己定义的视频名
完事。