如何从URL显示视频

时间:2013-10-06 16:19:59

标签: android android-videoview

我的应用主要标签上有一个按钮,尝试通过第二个活动从网址启动视频

我已经使用空白活动进行了测试,并在单击所述按钮时成功显示“Hello”。然后我删除了'Hello'edite文本,并在另一个类似的帖子中建议将代码添加到java文件中。

点击视频btn时,我收到消息;无法播放此视频,确定'

我已经测试了youtube链接,它是一个有效的链接。然而,logcat错误消息包括:

10-07 09:04:36.785: I/MediaPlayer(11397): path is null
10-07 09:04:36.795: D/MediaPlayer(11397): setDataSource IOException happend : 
10-07 09:04:36.795: D/MediaPlayer(11397): 
java.io.FileNotFoundException: No content provider: http://youtu.be/RFrg1xFoxeQ 

我从哪里开始?

第二项活动的XML如下: -     

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</TableLayout>

对应的Java文件

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_serve_motion_up_together);
    String videoPath2 = "http://youtu.be/RFrg1xFoxeQ";
    VideoView videoView1 = (VideoView) findViewById(R.id.videoView1);

    videoView1.setVideoURI(Uri.parse(videoPath2));
    videoView1.setMediaController(new MediaController(this));
    videoView1.setVisibility(1);
    videoView1.bringToFront();
    videoView1.requestFocus();
    videoView1.start();       

}

logcat的

10-07 09:04:36.765: D/dalvikvm(11397): GC_CONCURRENT freed 97K, 12% free 9619K/10887K, paused 13ms+2ms, total 47ms
10-07 09:04:36.785: V/MediaPlayer(11397): constructor
10-07 09:04:36.785: V/MediaPlayer(11397): setListener
10-07 09:04:36.785: I/MediaPlayer(11397): path is null
10-07 09:04:36.795: D/MediaPlayer(11397): setDataSource IOException happend : 
10-07 09:04:36.795: D/MediaPlayer(11397): java.io.FileNotFoundException: No content provider: http://youtu.be/RFrg1xFoxeQ
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:710)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:614)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.media.MediaPlayer.setDataSource(MediaPlayer.java:929)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.widget.VideoView.openVideo(VideoView.java:235)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.widget.VideoView.access$2000(VideoView.java:51)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.widget.VideoView$6.surfaceCreated(VideoView.java:478)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.SurfaceView.updateWindow(SurfaceView.java:591)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.SurfaceView.access$000(SurfaceView.java:81)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:173)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:707)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1944)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1113)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4481)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.Choreographer.doFrame(Choreographer.java:525)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.os.Handler.handleCallback(Handler.java:615)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.os.Looper.loop(Looper.java:137)
10-07 09:04:36.795: D/MediaPlayer(11397):   at android.app.ActivityThread.main(ActivityThread.java:4867)
10-07 09:04:36.795: D/MediaPlayer(11397):   at java.lang.reflect.Method.invokeNative(Native Method)
10-07 09:04:36.795: D/MediaPlayer(11397):   at java.lang.reflect.Method.invoke(Method.java:511)
10-07 09:04:36.795: D/MediaPlayer(11397):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
10-07 09:04:36.795: D/MediaPlayer(11397):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
10-07 09:04:36.795: D/MediaPlayer(11397):   at dalvik.system.NativeStart.main(Native Method)
10-07 09:04:36.795: D/MediaPlayer(11397): Couldn't open file on client side, trying server side
10-07 09:04:36.795: V/MediaPlayer(11397): setVideoSurfaceTexture
10-07 09:04:36.795: V/MediaPlayer(11397): MediaPlayer::setAudioStreamType
10-07 09:04:36.795: V/MediaPlayer(11397): setVideoSurfaceTexture
10-07 09:04:36.795: V/MediaPlayer(11397): prepareAsync
10-07 09:04:36.825: D/ProgressBar(11397): setProgressDrawable mProgressDrawable = null, d = android.graphics.drawable.LayerDrawable@41b6d730needUpdate = false
10-07 09:04:36.825: D/ProgressBar(11397): setProgress = 0
10-07 09:04:36.825: D/ProgressBar(11397): setProgress = 0, fromUser = false
10-07 09:04:36.825: D/ProgressBar(11397): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 100
10-07 09:04:36.835: W/ResourceType(11397): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
10-07 09:04:36.935: E/SpannableStringBuilder(11397): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-07 09:04:36.935: E/SpannableStringBuilder(11397): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-07 09:04:40.699: V/MediaPlayer(11397): message received msg=100, ext1=1, ext2=-2147483648
10-07 09:04:40.699: E/MediaPlayer(11397): error (1, -2147483648)
10-07 09:04:40.699: V/MediaPlayer(11397): callback application
10-07 09:04:40.699: V/MediaPlayer(11397): back from callback
10-07 09:04:40.699: E/MediaPlayer(11397): Error (1,-2147483648)
10-07 09:04:40.699: D/VideoView(11397): Error: 1,-2147483648
10-07 09:04:40.719: W/ResourceType(11397): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)

TIA

CCH4ADT

2 个答案:

答案 0 :(得分:1)

试试这个:

String LINK = "http://youtu.be/RFrg1xFoxeQ";
setContentView(R.layout.mediaplayer);
VideoView videoView = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();

我不确定它会起作用,但值得一试

答案 1 :(得分:0)

您不能简单地将YouTube网址添加到MediaPlayer。毕竟,这是指向某个网页的链接。 Android正试图处理这个问题,尽管该网址没有ContentProvider(架构,域等)。如果您想在应用中播放YouTube视频,请查看YouTube SDK for Android

最好的问候, Darek