Play a video on Youtube using an Intent

Posted by Marco Dinacci on 0 comments

Warning! For this demo to work the user needs the Youtube application installed on his device.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    String video_path = "http://www.youtube.com/watch?v=opZ69P-0Jbc";
    Uri uri = Uri.parse(video_path);
        
    // With this line the Youtube application, if installed, will launch immediately.
    // Without it you will be prompted with a list of the application to choose.
    uri = Uri.parse("vnd.youtube:"  + uri.getQueryParameter("v"));
        
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

The example uses a standard Youtube URL. The

uri.getQueryParameter("v")

is used to extract the video ID from the URI itself, in this example the ID is opZ69P-0Jbc