How do you rate this blog

Saturday, July 12, 2014

YouTube player in Android application

In the previous post I have described about a normal video player and streaming videos through it. In this post we will see how to stream youtube videos.

To obtain API key for accessing for youtube data api v3:


Step1: Logon to google developer console: Console
Step2: Once you login create a new project
Step3: On creation of new project go to API's and turn on You Tube API V3
Step4: Once this is done, click on credentials to generate a key for the API which we selected in the step above for android. Click on Create new key under Public API

access for android once it is done it asks you to create SHA1 fingerprint to do this execute the below command in command prompt

keytool.exe -V -list -alias androiddebugkey -keystore "<the directory in which debug.keystore is stored>" -storepass android -keypass android

example
keytool.exe -V -list -alias androiddebugkey -keystore "C:\Users\Bharath\.android\debug.keystore" -storepass android -keypass android

Once done copy the SHA1 and append the package name in which you will embed the api as show below
<SHA1 Key>;<package name of your solution>

To create the application:


Step1: Download the youtube api jar file from : Download
Step2: Add the jar file into the application
    a) Right click on the project and click on properties
    b) Click on java build path
    c) Click on libraries
    d) Add external jar
    e) Add YouTubeAndroidPlayerAPI.jar
    f) click on the text box next to the jar file in order and export tab

Step3: To allow internet access-
Add the code below into Android Manifest file
<uses-permission android:name="android.permission.INTERNET" />

Step4: Add the youtube player to the application:

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Step5: Code for youtube player in your application


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

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;

public class Youtubevids extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

    private YouTubePlayer myPlayer;
    private static final String myAPIKey ="<You Developer Key>";
    private static final int RECOVERY_DIALOG_REQUEST = 1;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtubevids);
        //Youtube player
        YouTubePlayerView myPlayer=(YouTubePlayerView) findViewById(R.id.youtube_view);
        myPlayer.initialize(myAPIKey, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.youtubevids, menu);
        return true;
    }

    @Override
    public void onInitializationFailure(Provider prov,
            YouTubeInitializationResult res) {
        // if there is an error
        if(res.isUserRecoverableError()){
            res.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        }else {
            Toast.makeText(this, "Error: "+res.toString(), Toast.LENGTH_LONG).show();
        }
    }
   
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getPlayerProvider().initialize(myAPIKey, this);
        }
    }

    protected Provider getPlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }
   
   
    @Override
    public void onInitializationSuccess(Provider prob, YouTubePlayer player,
            boolean restore) {
    if(!restore){
        myPlayer=player;
        //The video which you want to play, you can use a list but for this post I am using a video.
        myPlayer.cueVideo("QQr7gM8MA3s");
    }
       
    }

}

Run the application:

Once this is done you are ready to run the app, you can run it on the emulator, but it requires youtube app to be installed in the emulator if you want to test it. I

have run this directly on mobile and the output is as shown in the snapshot given below.


1 comment: