4

I am trying to play a youtube video in a Video View.

I have laid out the xml like this:

<VideoView 
            android:id="@+id/VideoView"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent" 
            />

and the code is like this:

setContentView(R.layout.webview);
        VideoView vv = (VideoView) findViewById(R.id.VideoView);                        
        MediaController mc=new MediaController(this);
        mc.setEnabled(true);
        mc.show(0);
        vv.setMediaController(mc); 
        vv.setVideoURI(Uri.parse("http://www.youtube.com/watch?v=XS998HaGk9M"));
        vv.requestFocus();
        vv.showContextMenu();
        vv.start();  

I have added the permission within the manifest. When I load the application a dialog appears stating the video cannot be played.

I would appreciate any advice on this. Thanks

enter image description here

Raj
  • 693
  • 4
  • 17
  • 29

3 Answers3

18

You specified wrong URI for the video. http://www.youtube.com/watch?v=XS998HaGk9M is a web page, but not directly a video stream

Here is correct URI example:

rtsp://v6.cache4.c.youtube.com/CigLENy73wIaHwmh5W2TKCuN2RMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp

Also, this address can be obtained from YouTube API. For example, from here: http://gdata.youtube.com/feeds/api/users/phonedog/uploads

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
Eugene Nacu
  • 1,613
  • 2
  • 14
  • 22
  • sorry but i am not getting how to get this url ? and i want to play video in native video palyer not in you tube. by that way i can do. so is it possible ? – Chintan Khetiya Dec 04 '12 at 10:10
  • Sorry .this is only done with 3gp. that will convert each and every video to 3gp. is there any other way that we can play some other type of video with native player. i don't want you tube tag in my screen. hope you get it – Chintan Khetiya Dec 04 '12 at 11:37
3

While not an explicit answer I believe you need to launch an intent with a YouTube URL and let the OS handle it. That is, I don't think you can embed YouTube videos directly into your activities though I would love to be proven wrong.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • 1
    While there is no public api to invoke the youTube app, you can do so. You should be aware that doing so is potentially fragile, but very likely the only way you can play youtube videos on an android device. – Nick Campion Mar 02 '11 at 04:48
  • 1
    I wouldn't say fragile. You can code to support it or not but this is what intents were meant to solve. Launch the intent and catch any failed-to-handle exceptions and give a sorry-you-phone-does-not-play-youtube-videos message. In my experience, nearly all commercial phones have YouTube players. – Andrew White Mar 02 '11 at 18:36
2

Android webview and videoviews do not support play back of youtube videos in my experience.

Nick Campion
  • 10,479
  • 3
  • 44
  • 58