Chapter 10. Multimedia

10.1. Introduction: Multimedia

Ian Darwin


Android is a rich multimedia environment. The standard Android load includes music and video players, and most commercial devices ship with these or fancier versions as well as YouTube players and more. The recipes in this chapter show you how to control some aspects of the multimedia world that Android provides.

10.2. Playing a YouTube Video

Marco Dinacci


You want to play a video from YouTube on your device.


Given a URI to play the video, create an ACTION_VIEW Intent with it and start a new Activity.


Example 10-1 shows the code required to start a YouTube video with an Intent.


For this recipe to work, the user needs the standard YouTube application installed on the device.

Example 10-1. Starting a YouTube video with an Intent
public void onCreate(Bundle savedInstanceState) {
    String video_path = "";
    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(""  + uri.getQueryParameter("v"));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

The example uses a standard URL. The uri.getQueryParameter("v") is used to extract the video ID from the URI itself; in our example the ID is opZ69P-0Jbc ...

Get Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.