We can play youtube videos inside our android application using VideoView.
VideoView displays a video file. The VideoView class can load images from various sources , takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
Using Video View:
1.Code in the layout xml:
<VideoView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/YoutubeVideoView" />
2.Code in java class:
VideoView v = (VideoView) findViewById(R.id.YoutubeVideoView); v.setVideoURI(Uri .parse("rtsp://v4.cache3.c.youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp")); v.setMediaController(new MediaController(this)); //sets MediaController in the video view // MediaController containing controls for a MediaPlayer v.requestFocus();//give focus to a specific view v.start();//starts the video
We set the VideoUri by specifying the 3gp link of Youtube video for mobile platforms. To add media controls such as Play, Pause, Rewind, Fast Forward and a progress slider ,we add MediaController to the VideoView.
How to get 3gp link of a Youtube video
- Right click the video to copy Video URL.
- Open “m.youtube.com” to get the mobile version videos.
- Right click on the video to Copy Link Address.
- The 3gp link of the video looks like this :
The 3gp link of the video looks like this :
rtsp://v4.cache3.c.youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp
Playing youtube videos in android application was that simple !
Download the complete Demo project here for your reference . videoTEST