Apr 6, 2013

Display YouTubeThumbnailView of YouTube Android Player API

Before start coding using YouTube Android Player API, read the post to prepare your development tools.

Example:

YouTubeThumbnailView
YouTubeThumbnailView


package com.example.androidyoutubethumbnailview;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;

public class MainActivity extends Activity 
 implements YouTubeThumbnailView.OnInitializedListener{
 
 public static final String DEVELOPER_KEY = "replace your own API Key here";
 private static final String VIDEO_ID = "fhWaJi1Hsfo";

 private YouTubeThumbnailLoader youTubeThumbnailLoader;
 private YouTubeThumbnailView thumbnailView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  thumbnailView = (YouTubeThumbnailView)findViewById(R.id.thumbnailview);
  thumbnailView.initialize(DEVELOPER_KEY, this);
 }

 @Override
 public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
   YouTubeInitializationResult errorReason) {
  
  String errorMessage =
    String.format("onInitializationFailure (%1$s)", 
      errorReason.toString());
  Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); 
 }

 @Override
 public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
   YouTubeThumbnailLoader thumbnailLoader) {
  
  Toast.makeText(getApplicationContext(), 
         "onInitializationSuccess", Toast.LENGTH_SHORT).show();
  
  youTubeThumbnailLoader = thumbnailLoader;
     thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailListener());
     
     youTubeThumbnailLoader.setVideo(VIDEO_ID);
 }
 
   private final class ThumbnailListener implements
       YouTubeThumbnailLoader.OnThumbnailLoadedListener {

     @Override
     public void onThumbnailLoaded(YouTubeThumbnailView thumbnail, String videoId) {
       Toast.makeText(getApplicationContext(), 
         "onThumbnailLoaded", Toast.LENGTH_SHORT).show();
     }

     @Override
     public void onThumbnailError(YouTubeThumbnailView thumbnail,
         YouTubeThumbnailLoader.ErrorReason reason) {
      Toast.makeText(getApplicationContext(), 
        "onThumbnailError", Toast.LENGTH_SHORT).show();
     }  
   }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <com.google.android.youtube.player.YouTubeThumbnailView
        android:id="@+id/thumbnailview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


No comments:

Post a Comment

Infolinks In Text Ads