我正在使用MediaPlayer尝试从youtube流式传输视频和音频。
我如何使用网址进行此操作:
示例:
http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec
编辑:使用你的方法时得到的LogCat错误。
09-04 22:22:30.140: INFO/AwesomePlayer(85): setDataSource_l('http://www.youtube.com/watch?v=I3jv0IF9n6A')
09-04 22:22:30.140: INFO/NuHTTPDataSource(85): connect to www.youtube.com:80/watch?v=I3jv0IF9n6A @0
09-04 22:22:30.250: INFO/NuHTTPDataSource(85): connect to m.youtube.com:80/watch?desktop_uri=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DI3jv0IF9n6A&v=I3jv0IF9n6A&gl=US @0
09-04 22:22:30.410: INFO/NuHTTPDataSource(85): connect to m.youtube.com:80/#/watch?desktop_uri=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DI3jv0IF9n6A&v=I3jv0IF9n6A&gl=US @0
09-04 22:22:30.570: INFO/NuHTTPDataSource(85): Chunked transfer encoding applied.
09-04 22:22:30.570: WARN/NuHTTPDataSource(85): Server did not give us the content length!
09-04 22:22:31.000: INFO/NuCachedSource2(85): ERROR_END_OF_STREAM
09-04 22:22:31.630: ERROR/MediaPlayer(8404): error (1, -2147483648)
09-04 22:22:31.630: WARN/System.err(8404): java.io.IOException: Prepare failed.: status=0x1
09-04 22:22:31.630: WARN/System.err(8404): at android.media.MediaPlayer.prepare(Native Method)
09-04 22:22:31.630: WARN/System.err(8404): at com.fttech.example.youtube.example.AccessibleYouTube.watchVideo(AccessibleYouTube.java:139)
09-04 22:22:31.630: WARN/System.err(8404): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 22:22:31.630: WARN/System.err(8404): at java.lang.reflect.Method.invoke(Method.java:491)
09-04 22:22:31.630: WARN/System.err(8404): at android.view.View$1.onClick(View.java:2678)
09-04 22:22:31.630: WARN/System.err(8404): at android.view.View.performClick(View.java:3110)
09-04 22:22:31.630: WARN/System.err(8404): at android.view.View$PerformClick.run(View.java:11928)
09-04 22:22:31.630: WARN/System.err(8404): at android.os.Handler.handleCallback(Handler.java:587)
09-04 22:22:31.630:WARN / System.err(8404):在android.os.Handler.dispatchMessage(Handler.java:92)
答案 0 :(得分:1)
如果您想使用MediaPlayer
对象,则以下调用将对您有效:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec");
mp.prepare();
mp.start();
否则,如果您不介意接收YouTube应用,请使用以下代码:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec")));
答案 1 :(得分:0)
Uri uri = Uri.parse("http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec");
Intent i = new Intent("android.intent.action.VIEW");
i.setDataAndType(uri, "video/*");
startActivity(i);