I have the following code:
VideoView videoView = (VideoView)findViewById(R.id.instructionsvideo);
assert videoView != null;
videoView.setVideoPath("android.resource://" + getPackageName() + R.raw.testnatureclip);
videoView.start();
"testnatureclip" is located in the raw folder:
For some reason after I build the project the file turns red.
Here is the error I get:
com.roymunson.vroy.copypastakeyboard W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No package found for authority: android.resource://com.roymunson.vroy.copypastakeyboard2131165184
The mp4 should be encoded in H.264 format, but I don't know if the online encoding service I used worked.
Additionally, the videoview does not have the same dimensions as the file, if that matters.
What is the problem? Is the file path incorrect, or I am I missing some element in initializing the videoview?
UPDATE ONE:
Using User8's solution I got the following error:
roymunson.vroy.copypastakeyboard W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: /2131165184
10-01 17:36:20.912 28156-28156/com.roymunson.vroy.copypastakeyboard W/VideoView: Unable to open content: /2131165184
java.io.IOException: setDataSource failed.
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1100)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1074)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1028)
at android.widget.VideoView.openVideo(VideoView.java:346)
at android.widget.VideoView.-wrap0(VideoView.java)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:623)
at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
I found 2 solutions:
String uriPath = "android.resource://" + getPackageName() + "/raw/testnatureclip";
Uri uri = Uri.parse(uriPath);
videoView.setVideoURI(uri);
or
videoView.setVideoURI(Uri.parse("android.resource://ABSOLUTE_PACKAGE_NAME/" + R.raw.testnatureclip));
Try this one out, specifying an absolute package name path and using Uri:
videoView.setVideoURI(Uri.parse("android.resource://com.roymunson.vroy.copypastakeyboard/"
+ R.raw.testnatureclip));
Also, why don't you just use getPackageResourcePath()
directly to access resources?
Then, you generally can't access them that way, you should use only their resourceID
, here is a discussion on exactly that topic: Access resource files in Android.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With