Sapan Diwakar

Software developer

Follow me on Twitter Check out my code on GitHub View some of my designs on Dribbble Take a look at my Linked In profile

Streaming Youtube Videos and Loading Thumbnails in Android

The only "official" way of streaming videos from Youtube in a mobile app is to use an iframe. Since the WebViews on mobile devices aren't as good as their desktop counterparts, this is filled with bugs and isn't very easy to operate on. Fortunately, Google has a YouTube Android Player API that enables you to incorporate video playback functionality into your Android applications. The API itself is very easy to use and works well. For example, here is how to create a new activity to play a video using the API.

Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, "<<YOUTUBE_API_KEY>>", "<<Youtube Video ID>>", 0, true, false);  

The API even provides a YouTubeThumbnailView to make it easy to display thumbnails in the application. As opposed to the YouTubeStandalonePlayer which works pretty well for playing videos, YouTubeThumbnailView is ridden with bugs. The recommended way to display YouTube thumbnails is to place a YouTubeThumbnailView in your view hierarchy and then use the YouTubeThumbnailLoader to load a single video thumbnail or a whole playlist. Use multiple loaders/views in the app and you will soon have plenty of errors

java.lang.IllegalStateException: This YouTubePlayer has been released ...  
java.lang.IllegalStateException: android.os.DeadObjectException ...  
java.lang.IllegalArgumentException: Service not registered: [email protected] ...  

A much better and stable approach is to use the YouTube Data API Client Library for Java to request the thumbnail urls separately and then use something like Universal Image Loader or Picasso to load the images in ImageView. Including the API client requires quite a bit of jars to be included in the package. First, download the library from here. Modify your build.gradle to include the dependencies

    compile files('libs/commons-logging-1.1.1.jar')
    compile files('libs/google-api-client-1.19.0.jar')
    compile files('libs/google-api-client-android-1.19.0.jar')
    compile files('libs/google-api-services-youtube-v3-rev113-1.19.0.jar')
    compile files('libs/google-http-client-1.19.0.jar')
    compile files('libs/google-http-client-android-1.19.0.jar')
    compile files('libs/google-http-client-gson-1.19.0.jar')
    compile files('libs/google-oauth-client-1.19.0.jar')
    compile files('libs/google-oauth-client-java6-1.19.0.jar')
    compile files('libs/gson-2.1.jar')
    compile files('libs/httpclient-4.0.1.jar')
    compile files('libs/httpcore-4.0.1.jar')
    compile files('libs/jsr305-1.3.9.jar')

Then load the thumbnails for all videos by passing their ids in a Youtube.Videos.List query. Make sure that you use the browser key for this request. The mobile app key that you used for youtube player in the previous step would not work with this library.

try {  
    YouTube.Videos.List videoListQuery = youtube.videos().list("contentDetails,snippet,id").setId(Joiner.on(',').join(youtubeVideoIds)).setKey("<<YOUTUBE_API_KEY_BROWSER>>");
    VideoListResponse videoListResponse = videoListQuery.execute();
    ytVideos = videoListResponse.getItems();
} catch (IOException e) {
    Log.w(TAG, "Cannot get video thumbnails", e);

Each Youtube Video now contains the thumbnail url. Here's how to get it.

String url = youtubeVideo.getSnippet().getThumbnails().getDefault().getUrl();  
UrlImageViewHelper.setUrlDrawable(thumbnailView, url); // Use any image loader library like Universal Image Loader, Picasso etc.