Android Playing YouTube Video without third-party Apps

Have you tried to play a YouTube video in your App without any third-party application including YouTube App? Forget about everything and this is working perfectly. Follow these easy steps;



First we need RTSP protocol URL. You can easily find RTSP link using following URL;

http://gdata.youtube.com/feeds/mobile/videos/<your_video_id>



Once you paste this on a new tab of your webbrowser; it will print something similar to following;

<?xml version='1.0' encoding='UTF-8'?><media:group>
<media:category label='Film &amp; Animation' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Film
</media:category><media:content 
url='rtsp://r3---sn-a5m7zu76.c.youtube.com/CiILENy73wIaGQnup-1SztVOYBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' isDefault='true' expression='full' duration='597' yt:format='1'/>
<media:content url='rtsp://r3---sn-a5m7zu76.c.youtube.com/CiILENy73wIaGQnup-1SztVOYBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='597' yt:format='6'/>
<media:description type='plain'>Big Buck Bunny tells the story of a giant rabbit with a heart bigger than himself. 
When one sunny day three rodents rudely harass him, something snaps... 
and the rabbit ain't no bunny anymore! In the typical cartoon tradition he prepares the nasty rodents a comical revenge...

Note that highlighted link. It is your RSTP protocol link to your video. Then we need to create a seperate activity called `VideoActivity.java`.

VideoActivity.java
public class VideoActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_video);
 
 Bundle extras = getIntent().getExtras();
 if (extras != null) {
  String vURL = extras.getString("EXTRA_VIDEO_URL");
  VideoView videoView = (VideoView) findViewById(R.id.videoView1);
  videoView.setVideoPath(vURL);
  MediaController mediaController = new MediaController(this);
  mediaController.setAnchorView(videoView);
  videoView.setMediaController(mediaController);
  videoView.start();
 }
}
}

Create this layout `activity_video.xml` on res/layout folder.
activity_video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

***Don't forget to add `VideoActivity` into AndroidManifest.xml. It should be between <application> tags.

 <activity android:name="example.test.VideoActivity" 
            android:label="Video">
 </activity>

Now you can call this activity as follows;

//define extra parameter
String vURL="rtsp://r3---sn-a5m7zu76.c.youtube.com/CiILENy73wIaGQnup-1SztVOYBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
//start activity passing extras
Intent intent = new Intent(this,VideoActivity.class);
intent.putExtra("EXTRA_VIDEO_URL", vURL);
startActivity(intent);
SHARE

Rasika Perera

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment