简体   繁体   English

Android 在 VideoView 中加载视频

[英]Android load video in VideoView

I want to load a video in a video view from raw folder with the following code我想使用以下代码从原始文件夹中的视频视图中加载视频

 String uri = "android.resource://" + getPackageName() + "/" + R.raw.preview;
 VideoView mVideoView  = (VideoView)findViewById(R.id.videoView1);
 mVideoView.setVideoURI(Uri.parse(uri));
 mVideoView.requestFocus();
 mVideoView.start();

I receive NullPointerException at this line: mVideoView.setVideoURI(Uri.parse(uri));我在这一行收到NullPointerExceptionmVideoView.setVideoURI(Uri.parse(uri)); Any ideas in what should I do ?我应该怎么做的任何想法?

Make sure the findViewById function call is returning a VideoView object and is not null.确保findViewById函数调用返回一个 VideoView 对象并且不为空。

Null pointer errors typically happen when you call an method to a object that is null.当您对空对象调用方法时,通常会发生空指针错误。

Chances are the reference to R.id.videoView1 in your layout xml file is wrong or you could have an error in your xml layout file that isn't showing up. R.id.videoView1是您的布局 xml 文件中对R.id.videoView1的引用是错误的,或者您的 xml 布局文件中可能存在未显示的错误。

If you're using Eclipse or Android Studio, the RivideoView1 should be blue, showing that it was found in the layout file.如果您使用的是 Eclipse 或 Android Studio,则RivideoView1应为蓝色,表明它已在布局文件中找到。

Also you can verify the object isn't null before calling the methods to be sure.您也可以在调用方法之前验证对象不为空。 See below:见下文:

 String uri = "android.resource://" + getPackageName() + "/" + R.raw.preview;
 VideoView mVideoView  = (VideoView)findViewById(R.id.videoView1);
 if (mVideoView != null)
 {  mVideoView.setVideoURI(Uri.parse(uri));
    mVideoView.requestFocus();
    mVideoView.start();
 } else
 { //toast or print "mVideoView is null"
 }

Try this:尝试这个:

String path = "android.resource://" + getPackageName() + "/" + R.raw.preview;
VideoView mVideoView  = (VideoView)findViewById(R.id.videoView1);
mVideoView.setVideoPath(path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM