Create a YouTube Client on Android

Final product image
What You'll Be Creating

There are a lot of popular third party YouTube clients on Google Play, such as Viral Popup and PlayTube, that manage to offer some unique and additional functionality that the official YouTube app doesn't. If you want to build one such app yourself, this tutorial is for you.

In this tutorial, we create our own YouTube client that cannot only search for videos on YouTube, but also play them. Doing so, we will learn how to make use of the YouTube Android Player API and the YouTube Data API client library for Java.

Prerequisites

Ensure that you have the latest Eclipse ADT Bundle set up. You can download it at the Android Developer website.

You must also have a developer key to use the YouTube API. Follow the steps on Google's YouTube Developer website to get one.

1. Create a New Project

Fire up Eclipse and create a new Android application. Name the application, SimplePlayer. Choose a unique package name, and set the minimum required SDK to Android 2.2 and the target SDK to Android 4.X (L Preview).

We're going to create the Activity ourselves so deselect Create Activity and click Finish.

2. Adding Libraries

Step 1: Download Libraries

You will need the following libraries for this project:

  • YouTube Android Player API: This library lets your app embed and control YouTube videos seamlessly. At the time of writing, the latest version of this library is 1.0.0. You can download it from the Google Developers website.
  • YouTube Data API v3 Client Library for Java: This library lets your app query information on YouTube. We are going to use it to enable our app to search for videos on YouTube. This is also available on the Google Developers website.
  • Picasso: This library makes it easy to fetch and display remote images. We are going to use it to fetch thumbnails of YouTube videos. The latest version currently is 2.4.0 and you can download it directly from the Maven repository.

Step 2: Add Libraries

To add the YouTube Android Player API, unzip YouTubeAndroidPlayerApi-1.0.0.zip and copy the file YouTubeAndroidPlayerApi.jar to the libs folder of your project.

To add the YouTube Data API v3 library and its dependencies, unzip google-api-services-youtube-v3-rev124-java-1.19.0.zip and copy the following files to the libs folder of your project:

  • google-api-services-youtube-v3-rev124-1.19.0.jar
  • google-api-client-1.19.0.jar
  • google-oauth-client-1.19.0.jar
  • google-http-client-1.19.0.jar
  • jsr305-1.3.9.jar
  • google-http-client-jackson2-1.19.0.jar
  • jackson-core-2.1.3.jar
  • google-api-client-android-1.19.0.jar
  • google-http-client-android-1.19.0.jar

Finally, to add Picasso, copy picasso-2.4.0.jar to the libs folder of your project.

3. Edit the Manifest

The only permission our app needs is android.permission.INTERNET to access YouTube's servers. Add the following to AndroidManifest.xml:

Our app has two activities, one to search for videos and one to play them. To avoid having to handle orientation changes in this tutorial, we force both the activities to only use landscape mode. Declare the activities in the manifest by adding the following code to it:

4. Edit strings.xml

The res/values/strings.xml file contains the strings that our app uses. Update its contents as shown below:

5. Create Layout for SearchActivity

Step 1: Create Layout

SearchActivity needs the following views:

  • EditText: to allow the user to type in the search keywords
  • ListView: to display the search results
  • LinearLayout: this view serves as the parent view of the aforementioned views

Create a new file named layout/activity_search.xml and add the following code to it:

Step 2: Layout Out Search Results

Each search result refers to a video on YouTube and we need a layout to display information about that video. Therefore, each item of the ListView needs to contain the following views:

  • ImageView: to display the thumbnail of the video
  • TextView: to display the title of the video
  • TextView: to display the description of the video
  • RelativeLayout: this view acts as the parent view of the other views

Create a file named layout/video_item.xml and add the following code to it:

6. Create Layout for PlayerActivity

Step 1: Create Layout

PlayerActivity needs the following views:

  • YouTubePlayerView: to play YouTube videos
  • LinearLayout: this view is the parent view of YouTubePlayerView

Create a new file named layout/activity_player.xml and add the following code to it:

7. Create VideoItem

Create a new Java class named VideoItem.java. We use this class to store the following information about a YouTube video:

  • YouTube ID
  • title
  • description
  • thumbnail URL

All of the above are stored as strings. After adding the getters and setters for each of them, VideoItem.java file should have the following contents:

8. Create a Helper Class

To avoid having to deal with the YouTube Data API directly in our Activity, create a new Java class and name it YoutubeConnector.java. This class has the following member variables:

  • an instance of the YouTube class that will be used for communicating with the YouTube API
  • an instance of YouTube.Search.List to represent a search query
  • the YouTube API key as a static String

We initialize the above variables in the constructor. To initialize the instance of YouTube, the YouTube.Builder class has to be used. The classes that will be responsible for the network connection and the JSON processing are passed to the builder.

Once initialized, its search method is used to create a search request. The list method is then used to mention the details we want in the search results. For this tutorial, we are going to need an id and snippet for each search result. From those, we extract the following fields:

  • id/videoId
  • snippet/title
  • snippet/description
  • snippet/thumbnails/default/url

The developer's API key needs to be sent with every search request. The setKey method is used for this purpose. We also use the setType method to restrict the search results to videos only. At this point, the class should look something like this:

Next, we create a method named search to perform the search based on the user's keywords. This method accepts the keywords as a String parameter. The query variable's setQ method is used to set the keywords.

We then run the query using its execute method. The results are returned in the form of a SearchListResponse instance. We iterate through the result items and create a new List of VideoItem objects, which will be the return value of this method. After adding appropriate error handling, the search method should look like this:

9. Create SearchActivity

Create a new class named SearchActivity.java. This class has fields that represent the views we mentioned in activity_search.xml. It also has a Handler to make updates on the user interface thread.

In the onCreate method, we initialize the views and add an OnEditorActionListener to the EditText to know when the user has finished entering keywords.

You must have noticed the call to the searchOnYoutube method. Let's define the method now. In this method, we create a new Thread to initialize a YoutubeConnector instance and run its search method. A new thread is necessary, because network operations cannot be performed on the the main user interface thread. If you forget to do this, you will face a runtime exception. Once the results are available, the handler is used to update the user interface.

In the updateVideosFound method, we generate an ArrayAdapter and pass it on to the ListView to display the search results. In the getView method of the adapter, we inflate the video_item.xml layout and update its views to display information about the search result.

The Picasso library's load method is used to fetch the thumbnail of the video and the into method is used to pass it to the ImageView.

Finally, we need a method that sets the OnItemClickListener of the ListView so that the user can click on a search result and watch the corresponding video. Let's name this method addClickListener and call it at the end of the onCreate method.

When an item in the list is tapped, we create a new Intent for the PlayerActivity and pass in the ID of the video. Once the Intent is created, the startActivity method is used to launch the PlayerActivity.

10. Create PlayerActivity

Create a new Java class named PlayerActivity.java that inherits from YouTubeBaseActivity. This is important, because only subclasses of the YouTubeBaseActivity can make use of the YouTubePlayerView.

This class has a single member variable that represents the YouTubePlayerView we mentioned in the activity_player.xml layout file. This is initialized in the onCreate method by invoking the initialize method of the YouTubePlayerView class, passing in the developer API key.

Next, our class needs to implement the OnInitializedListener interface to know when the initialization is complete. The interface has two methods, named onInitializationFailure and onInitializationSuccess.

In case of success, the cueVideo method is used to display the YouTube video. In case of failure, a Toast is shown that tells the user that the initialization failed.

This is what the PlayerActivity class should look like:

11. Compile and Run

Our YouTube client is now ready to be deployed to an Android device. Almost all popular Android devices have it installed, but make sure the YouTube app is installed—and up to date—on the device, because our app depends on it.

Once deployed, you should be able to type in a query to search for videos on YouTube and then click on a result to start playing the corresponding video.

Conclusion

You now know how to embed YouTube videos in your Android app. You have also learned how to use the Google API client library and interact with YouTube. The Android Player API provides a lot of methods to control the playback of the videos and you can use them to come up with very creative apps. Refer to the complete reference guide to learn more about the API.

Tags:

Comments

Related Articles