2011-09-05 81 views
3

我正在使用MediaPlayer嘗試從YouTube上傳輸視頻和音頻。如何從URL播放視頻?

我如何去這樣做使用的網址:

例子:

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)

回答

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"))); 
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);