Friday, 14 November 2014

How To Play YouTube Video

How To Play YouTube Videos

HI!
        Today i am going to show you "How To Play YouTube Videos"

You need to prepare your system for do this. First you have to download YouTube Android Player API , Then Go to your console and activate your YouTube Data API v3Finally. Activate your Project API key from your Credential . Now You can start the coding part.

Add the YouTube Android Player API into your project. Create a new Activity with extend of  YouTubePlayer.OnInitializedListener.

On the activity_my__tube.XML file call the YouTube Player View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/you_player"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

On the Activity Replace the extend class 

import android.os.Bundle;
import android.widget.Toast;
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.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class My_Tube extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
public static final String key_user = "__________(Your project api key)_______________";

public static final String key_video = "_________(Video Id _ what you want to play)____________";

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

@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(this, "Error While Playing!", Toast.LENGTH_SHORT).show();
}

@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
player.setPlayerStateChangeListener(state_Change_Listener);
player.setPlaybackEventListener(play_Back_Listener);
if (!wasRestored) {
player.cueVideo(key_video);
}
}

private PlaybackEventListener play_Back_Listener = new PlaybackEventListener() {

@Override
public void onBuffering(boolean arg0) {

}

@Override
public void onPaused() {

}

@Override
public void onPlaying() {

}

@Override
public void onSeekTo(int arg0) {

}

@Override
public void onStopped() {

}

};

private PlayerStateChangeListener state_Change_Listener = new PlayerStateChangeListener() {

@Override
public void onAdStarted() {

}

@Override
public void onError(ErrorReason arg0) {

}

@Override
public void onLoaded(String arg0) {

}

@Override
public void onLoading() {
}

@Override
public void onVideoEnded() {

}

@Override
public void onVideoStarted() {

}
};

}

Now you can ENJOY by playing your YouTube Videos on your Application..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

No comments:

Post a Comment