Getting java.io.FileNotFoundException when using VideoView
Asked Answered
M

3

5

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:

enter image description here

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)
Mistakable answered 1/10, 2016 at 21:12 Comment(0)
M
7

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));
Mistakable answered 1/10, 2016 at 21:47 Comment(2)
I was editing my answer at the time you posted it. +1 Yes, just use the Uri method. And about getPackageResourcePath(), try so String uriPath = getPackageResourcePath() + "/raw/testnatureclip"; to shorten code.Radmilla
this code does not work when you have multi module project. Look here: https://mcmap.net/q/273464/-how-to-play-videos-in-android-from-assets-folder-or-raw-folderMicra
R
2

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.

Radmilla answered 1/10, 2016 at 21:22 Comment(2)
I read the post you linked, but I don't understand how to use getPackageResourcePath() directly.Mistakable
Also I tried your solution and it didn't work, I'll post the stacktrace in an edit on my original post.Mistakable
J
0

Try get path from raw folder:

    String path = "android.resource://" + getPackageName() + "/" + R.raw.testnatureclip;

And I recommend using ExoPlayer instead of VideoView:

app gradle:

implementation 'com.google.android.exoplayer:exoplayer:2.10.8'

layout xml:

<com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

java code:

PlayerView videoView = findViewById(R.id.video_view);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
videoView.setPlayer(player);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"));
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(path));
// Prepare the player with the source.
player.prepare(videoSource);
player.setPlayWhenReady(true); 
Jaenicke answered 3/12, 2019 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.