How to play video from URL?
I am using MediaPlayer to try and stream video and audio from youtube.
How do i go about doing this using the url:
Example:
http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec
EDIT: LogCat Error i get when using your methods.
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-0开发者_如何学编程4 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): at android.os.Handler.dispatchMessage(Handler.java:92)
If you're wanting to use MediaPlayer
object then the following calls will work for you:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec");
mp.prepare();
mp.start();
Otherwise, if you don't mind having the YouTube app take over, you can use the following code:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec")));
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);
精彩评论