Showing posts with label YouTube Android Player API. Show all posts
Showing posts with label YouTube Android Player API. Show all posts

Sunday, June 30, 2013

Example to use YouTubePlayerFragment of YouTube Android Player API

Example to use YouTubePlayerFragment of YouTube Android Player API

Example to use YouTubePlayerFragment of YouTube Android Player API


Follow the step in "YouTube Android Player API step-by-step" to Download and import YouTube Android Player API, and Register your app using YouTube Android Player API and obtain API Key. To use YouTubePlayerFragment, you have to define android:minSdkVersion="11" or higher in your AndroidManifest.xml.

Modify /res/layout/activity_main.xml to define layout, with <fragment> of "com.google.android.youtube.player.YouTubePlayerFragment".
<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" >

    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:id="@+id/youtubeplayerfragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:autoLink="web" />
    
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/btnviewfullscreen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="View in Full Screen" />
            <TextView 
                android:id="@+id/videolog"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            
        </LinearLayout>
    </ScrollView>
    
</LinearLayout>


Create /res/layout-land/activity_main.xml to define layout in Landscape orientation.
<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="horizontal"
    tools:context=".MainActivity" >

    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:id="@+id/youtubeplayerfragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"/>
    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="http://android-er.blogspot.com/"
            android:textStyle="bold"
            android:layout_gravity="center_horizontal"
            android:autoLink="web" />
        
        <ScrollView 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <Button
                    android:id="@+id/btnviewfullscreen"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="View in Full Screen" />
                <TextView 
                    android:id="@+id/videolog"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
  </ScrollView>
    </LinearLayout>
</LinearLayout>


MainActivity.java
package com.example.androidyoutubeapiplayer;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends YouTubeBaseActivity implements 
 YouTubePlayer.OnInitializedListener{
 
 public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
 public static final String VIDEO_ID = "o7VVHhK9zf0";
 
 private YouTubePlayer youTubePlayer;
 private YouTubePlayerFragment youTubePlayerFragment;
 private TextView textVideoLog;
 private Button btnViewFullScreen;
 
 private static final int RQS_ErrorDialog = 1;
 
 private MyPlayerStateChangeListener myPlayerStateChangeListener;
 private MyPlaybackEventListener myPlaybackEventListener;
 
 String log = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager()
             .findFragmentById(R.id.youtubeplayerfragment);
        youTubePlayerFragment.initialize(API_KEY, this);

        textVideoLog = (TextView)findViewById(R.id.videolog);
        
        myPlayerStateChangeListener = new MyPlayerStateChangeListener();
        myPlaybackEventListener = new MyPlaybackEventListener();
        
        btnViewFullScreen = (Button)findViewById(R.id.btnviewfullscreen);
        btnViewFullScreen.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    youTubePlayer.setFullscreen(true);
   }});
    }

 @Override
 public void onInitializationFailure(Provider provider,
   YouTubeInitializationResult result) {
  
  if (result.isUserRecoverableError()) {
   result.getErrorDialog(this, RQS_ErrorDialog).show(); 
  } else {
   Toast.makeText(this, 
     "YouTubePlayer.onInitializationFailure(): " + result.toString(), 
     Toast.LENGTH_LONG).show(); 
  }
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,
   boolean wasRestored) {
  
  youTubePlayer = player;
  
  Toast.makeText(getApplicationContext(), 
    "YouTubePlayer.onInitializationSuccess()", 
    Toast.LENGTH_LONG).show();
  
  youTubePlayer.setPlayerStateChangeListener(myPlayerStateChangeListener);
  youTubePlayer.setPlaybackEventListener(myPlaybackEventListener);
  
  if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
      }

 }
 
 private final class MyPlayerStateChangeListener implements PlayerStateChangeListener {
  
  private void updateLog(String prompt){
   log +=  "MyPlayerStateChangeListener" + "\n" + 
     prompt + "\n\n=====";
   textVideoLog.setText(log);
  };

  @Override
  public void onAdStarted() {
   updateLog("onAdStarted()");
  }

  @Override
  public void onError(
    com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
   updateLog("onError(): " + arg0.toString());
  }

  @Override
  public void onLoaded(String arg0) {
   updateLog("onLoaded(): " + arg0);
  }

  @Override
  public void onLoading() {
   updateLog("onLoading()");
  }

  @Override
  public void onVideoEnded() {
   updateLog("onVideoEnded()");
  }

  @Override
  public void onVideoStarted() {
   updateLog("onVideoStarted()");
  }
  
 }
 
 private final class MyPlaybackEventListener implements PlaybackEventListener {
  
  private void updateLog(String prompt){
   log +=  "MyPlaybackEventListener" + "\n-" + 
     prompt + "\n\n=====";
   textVideoLog.setText(log);
  };

  @Override
  public void onBuffering(boolean arg0) {
   updateLog("onBuffering(): " + String.valueOf(arg0));
  }

  @Override
  public void onPaused() {
   updateLog("onPaused()");
  }

  @Override
  public void onPlaying() {
   updateLog("onPlaying()");
  }

  @Override
  public void onSeekTo(int arg0) {
   updateLog("onSeekTo(): " + String.valueOf(arg0));
  }

  @Override
  public void onStopped() {
   updateLog("onStopped()");
  }
  
 }

}

download filesDownload the files.

Download APK to try on your device.



The tutorial: YouTube Android Player API step-by-step

Thursday, June 20, 2013

Simple example using YouTube Android Player API

Simple example using YouTube Android Player API

Before create app using YouTube Android Player API, you have to "Download and import YouTube Android Player API" and "Register your app using YouTube Android Player API and obtain API Key".

Modify layout xml to add view of <com.google.android.youtube.player.YouTubePlayerView>.
<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="http://android-er.blogspot.com/"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:autoLink="web" />
    
    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtubeplayerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


Modify MainActivity.java, extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener.
package com.example.androidyoutubeapiplayer;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;

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

public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{
 
 public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
 public static final String VIDEO_ID = "o7VVHhK9zf0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);
    }

 @Override
 public void onInitializationFailure(Provider provider,
   YouTubeInitializationResult result) {
  Toast.makeText(getApplicationContext(), 
    "onInitializationFailure()", 
    Toast.LENGTH_LONG).show();
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,
   boolean wasRestored) {
  if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
      }
 }

}


Where API_KEY is the key obtained in the step of "Register your app using YouTube Android Player API and obtain API Key", VIDEO_ID is the ID of the video to play.

Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidyoutubeapiplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidyoutubeapiplayer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



download filesDownload the files.

Download APK to try on your device.

Next: YouTubeThumbnailView example of YouTube Android Player API

Related: YouTube Android Player API step-by-step

Tuesday, June 18, 2013

Register your app using YouTube Android Player API and obtain API Key

To use YouTube Android Player API in your app, you have to register it and obtain API key. This post show you the steps to do so.

- Login with your Google account in Google APIs Console.

- Pull-down the Slection on left, and click Create... under Other Projects.



- Enter name of your project.



- Enable YouTube Data API v3 to ON.



- And then click to select API Access, and you will found you API key. You have to insert it in your code to use YouTube Android Player API.





To develop Android apps with YouTube Android Player API, read the posts in YouTube Android Player API step-by-step.

Download and import YouTube Android Player API

YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.

I will show the steps to develop Android App with YouTube Android Player API step-by-step, please refer the post YouTube Android Player API step-by-step.

To develop Android apps with YouTube Android Player API, you have to download and import the YouTube Android Player API jar to your project.

- New a Android Application Project.

- Download and unzip YouTube Android Player API here.

- Copy the unzipped YouTubeAndroidPlayerApi.jar file to /libs folder in your project. The YouTubeAndroidPlayerApi.jar file should be in /libs sub-folder of your unzipped files.

import YouTubeAndroidPlayerApi.jar

- Right click your project, select , then select Java Build Path and click Add JARs... button.


- Select YouTubeAndroidPlayerApi.jar, and click OK.


Wednesday, June 12, 2013

YouTube Android Player API step-by-step

The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.

Using the API, you can load or cue videos into a player view embedded in your application's UI. You can then control playback programmatically. For example, you can play, pause, or seek to a specific point in the currently loaded video.

You can also register event listeners to get callbacks for certain events, such as the player loading a video or the player state changing. Finally, the API has helper functionality to support orientation changes as well as transitions to fullscreen playback.






Develop Android App with YouTube Android Player API step-by-step:

Related: