在Android中播放youtube视频

时间:2012-05-20 16:05:05

标签: android youtube video-streaming android-mediaplayer

我正在开发一个Android应用程序,我想从youtube播放流媒体视频。我看了这样的帖子:How to play YouTube video in my Android application? 我尝试了这个来源:

  startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=tsDYIgX_gDs")));

我在WebView中收到了视频,我可以听到声音但没有图片..这也不是我想要的,因为我希望视频在活动开始时自动启动,而不需要点击按钮播放(因为我还想添加一些计数器) ,计时器......用于其他治疗)。 然后,我尝试了MediaPlayer:

 String FILE_PATH="http://www.youtube.com/watch?v=tsDYIgX_gDs";
    MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(FILE_PATH);
    mp.prepare();
    mp.start();

通过mp.start();跳转到此处控制视频开始。但我有这些错误:

05-20 15:36:36.279: ERROR/HTTPStream(33): recv failed, errno = 11 (Try again)
05-20 15:36:36.808: ERROR/HTTPDataSource(33): HTTP request failed w/ http status 303
05-20 15:36:36.808: ERROR/HTTPDataSource(33): retrying connection failed
05-20 15:36:41.834: ERROR/HTTPStream(33): recv failed, errno = 11 (Try again)
05-20 15:36:48.389: ERROR/HTTPStream(33): recv failed, errno = 11 (Try again)
05-20 15:36:54.913: ERROR/HTTPStream(33): recv failed, errno = 11 (Try again)
05-20 15:36:54.913: ERROR/HTTPStream(33): recv failed, errno = 9 (Bad file number)
05-20 15:36:54.918: ERROR/HTTPStream(33): recv failed, errno = 9 (Bad file number)
05-20 15:36:54.918: ERROR/HTTPStream(33): recv failed, errno = 9 (Bad file number)
05-20 15:36:54.918: ERROR/MediaPlayer(6392): error (1, -2147483648)
05-20 15:36:55.219: ERROR/MediaPlayer(6392): start called in state 0
05-20 15:36:55.219: ERROR/MediaPlayer(6392): error (-38, 0)
05-20 15:36:55.228: ERROR/MediaPlayer(6392): Error (-38,0)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

尝试将开始意图更改为:

     startActivity(new Intent(Intent.ACTION_VIEW, 
Uri.parse(String.format("http://www.youtube.com/v/%s", 
url.substring("vnd.youtube:".length(),n)));

要进行更深入的学习,请尝试以下问题:ThisThis