Jumat, 05 April 2013

Example to use YouTubePlayerFragment of YouTube Android Player API

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

Example to use YouTubePlayerFragment
Example to use YouTubePlayerFragment


To use YouTubePlayerFragment in your app, android:minSdkVersion have to be set "11" or higher.

Modify activity_main.xml to include <fragment> of "com.google.android.youtube.player.YouTubePlayerFragment" in your layout.
<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" />
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="@+id/youtubeplayerfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>


Modify MainActivity.java, to extend YouTubeBaseActivity and implements YouTubePlayer.OnInitializedListener. Replace your the String DEVELOPER_KEY with your own API Key obtain in the post "Preparation for development with YouTube Android Player API".
package com.example.androidyoutubeplayerfragment;

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.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;

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

public class MainActivity extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener{

public static final String DEVELOPER_KEY = "replace your own API Key here";
private static final int RECOVERY_DIALOG_REQUEST = 1;
private static final String VIDEO_ID = "fhWaJi1Hsfo";

YouTubePlayerFragment myYouTubePlayerFragment;

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

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer (%1$s)",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DEVELOPER_KEY, this);
}
}

protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView)findViewById(R.id.youtubeplayerfragment);
}

}


0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved