sofo
sofo

Reputation: 103

play youtube video in android

I am working on an android app and i want to play streaming video from youtube. I read posts like this: How to play YouTube video in my Android application? I tried with that source:

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

i got video in WebView, i can heard sound but no pictures..Also that is not what i want because i want video to start automatically when the activity start, without clicking Button play(because i want also to add some counters,timers.. for other treatments). Then, i tried whith MediaPlayer:

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

Hopping here having a control on video start by mp.start(); . But i have these errors:

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)

Can someone help plz?

Upvotes: 0

Views: 1494

Answers (1)

eric.itzhak
eric.itzhak

Reputation: 16062

Try changing the Start intent to :

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

For deeper learning try out the following question : This, and This.

Upvotes: 1

Related Questions