我对视频的了解很少。尽管如此,我正在构建一个需要以下功能的应用程序:给定视频的 url,播放视频。这个看似简单的任务对我来说非常困难。这是我的代码:
XML:
<VideoView android:id="@+id/videoview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="@color/primary_dark"/>
JAVA:
private void setupVideoToPlay(Bundle savedInstanceState,View parent) { mVideoView=(VideoView)parent.findViewById(R.id.videoview); mMediaController=new MediaController(getContext()); mMediaController.setAnchorView(mVideoView); mVideoView.setMediaController(mMediaController); mVideoView.setVideoURI(Uri.parse(mVideoUrl)); mVideoView.requestFocus(); if(null != savedInstanceState ) { int position = savedInstanceState.getInt(VIDEO_SEEK_POSITION); mVideoView.seekTo(position); } else { mVideoView.start(); } }
And here is the error I am getting:
D/MediaPlayer﹕ setDataSource IOException happend :
java.io.FileNotFoundException: No content provider: https://www.youtube.com/watch?feature=player_detailpage&v=c4rSDxwHJI0
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1074)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:927)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:854)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1085)
at android.widget.VideoView.openVideo(VideoView.java:370)
at android.widget.VideoView.access$2100(VideoView.java:73)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:651)
at android.view.SurfaceView.updateWindow(SurfaceView.java:606)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:184)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:895)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2158)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1185)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6591)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
有趣的是,我可以短暂地看到 VideoView 底部的控件。但是没有视频播放,只显示 VideoView 背景。有谁知道我该如何解决这个问题?
放在一边
否则是否有已知的图书馆可以做到这一点?理想情况下,我希望能够使用 youtube 视频链接或任何其他视频链接。我看过 ExoPlayer 和 Protyposis,但它们的单词/概念都超出了我的理解范围;诸如 TrackRenderer 和 ChunkSampleSource 之类的词,仅举两个例子。此外,Youtube API 本身似乎有一些有问题的要求:设备必须安装 Youtube 应用程序,我必须提供 videoId 而不是视频的 url。 (我还没有深入了解是否可以从 url 中解析出 videoId)。
注意
我已经尝试过使用 Youtube 视频链接和非 Youtube 视频链接。对于 Youtube 链接,我右键单击视频本身以获取链接。
最佳答案
Does anyone know how I might fix this?
为 one of the supported media formats and served via one of the supported protocols 中的视频提供 Uri
,而不是网页的 Uri
。 https://www.youtube.com/watch?feature=player_detailpage&v=c4rSDxwHJI0是一个网页。
关于android - 给定 URL 在 android 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32749745/