Chapter 10. Multimedia
10.1. Introduction: Multimedia
Discussion
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
Problem
You want to play a video from YouTube on your device.
Solution
Given a URI to play the video, create an ACTION_VIEW
Intent
with it and start a new
Activity
.
Discussion
Example 10-1 shows the code required to start a
YouTube video with an Intent
.
Note
For this recipe to work, the user needs the standard YouTube application installed on the 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.com 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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.