我有以下代码:
VideoView videoView = (VideoView)findViewById(R.id.instructionsvideo);
assert videoView != null;
videoView.setVideoPath("android.resource://" + getPackageName() + R.raw.testnatureclip);
videoView.start();
" testnatureclip"位于原始文件夹中:
由于某种原因,在我构建项目后,文件变为红色。
这是我得到的错误:
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
mp4应该采用H.264格式编码,但我不知道我使用的在线编码服务是否有效。
此外,如果重要的话,视频视图的尺寸与文件不同。
有什么问题?文件路径是否不正确,或者我在初始化视频时错过了一些元素?
更新一次:
使用User8的解决方案我收到以下错误:
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)
答案 0 :(得分:2)
我找到了2个解决方案:
String uriPath = "android.resource://" + getPackageName() + "/raw/testnatureclip";
Uri uri = Uri.parse(uriPath);
videoView.setVideoURI(uri);
或
videoView.setVideoURI(Uri.parse("android.resource://ABSOLUTE_PACKAGE_NAME/" + R.raw.testnatureclip));
答案 1 :(得分:1)
尝试这个,指定绝对包名称路径并使用Uri:
videoView.setVideoURI(Uri.parse("android.resource://com.roymunson.vroy.copypastakeyboard/"
+ R.raw.testnatureclip));
另外,为什么不直接使用getPackageResourcePath()
来访问资源?
然后,您通常无法以这种方式访问它们,您应该只使用 他们的resourceID
,这里是关于该主题的讨论:Access resource files in Android。
答案 2 :(得分:0)
尝试从原始文件夹获取路径:
String path = "android.resource://" + getPackageName() + "/" + R.raw.testnatureclip;
我建议使用ExoPlayer代替 VideoView :
app gradle:
implementation 'com.google.android.exoplayer:exoplayer:2.10.8'
布局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代码:
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);