我正在使用Android Studio 1.5.2(最新的稳定版)作为完整的Android开发人员课程的一部分,当我试图在播放视频文件的模拟器中运行应用程序时,我正在运行错误“无法播放视频”。该文件是MP4,已放置在项目的res / raw文件夹结构中。我可以听到音频,但媒体控件不起作用,清除错误信息后,屏幕仍然是黑屏。
这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.demo_video);
MediaController myMediaController = new MediaController(this);
myMediaController.setAnchorView(videoView);
videoView.setMediaController(myMediaController);
videoView.start();
}
尝试以下代码
VideoView videoView = (VideoView) findViewById(R.id.videoView);
MediaController myMediaController = new MediaController(this);
myMediaController.setAnchorView(videoView);
videoView.setMediaController(myMediaController);
String uriPath = "android.resource://"+getPackageName()+"/raw/"+ "demo_video";
Uri UrlPath=Uri.parse(uriPath);
videoView.setVideoURI(UrlPath);