Creating Android app to play youtube videos

15/04/2017   03:46:51 PM
Hello in this android tutorial we create your own Android application which is  used to  view or watch YouTube videos on android device.For this tutorial first we need to download latest  youtube player API  from googles developers website.So lets start the tutorial....   
1. Get the Android API key 

Gets the SHA-1 fingerprint on your computer using java keytool.

For Windows, Execute the following command in cmd prompt

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

android API key for Creating Android app to play youtube videos

2. Go to Google Developer Console and select or create a new project.

https://accounts.google.com/ServiceLogin?service=cloudconsole&ltmpl=api&osid=1&passive=true&continue=https://console.developers.google.com/#identifier

 3. On the left sidebar, click on API manager/Google apis list display/under youtube apis/ YouTube Data API
Enable the status for YouTube Data API v3 which is by default disable
 

 4. On the left sidebar, click on Credentials and Create new key in API key option. API key will be created

 

genrate API key google developer console

 

Android app to play youtube videos

 

5. Click on credentials option which present left side of navigation bar. Click on name of API key.

 

Creating Android app to play youtube videos

 
6. Select Android App option.
 
7. Paste the SHA-1 key created through cmd and your project’s package name separated by semicolon(;).
 
8. Click on save. Now you should see the API KEY on the dashboard.
 
9. Create a new project in android studio
 
10. Download the latest of version of YouTube Android Player API by click on the link https://developers.google.com/youtube/android/player 

and extract it. After extracted, you find YouTubeAndroidPlayerApi.jar file inside libs folder.

 
11. Paste the YouTubeAndroidPlayerApi.jar file in your project’s libs folder.
 

12. Add the following code in activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.xperiaitinnovations.ambc.fasttrackmaths.TeachingVideos">
   <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_viewtraining"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        />
</RelativeLayout>
 
12. Code in MainActivity.java
 
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
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.YouTubePlayerView;
public class TeachingVideos   extends YouTubeBaseActivity implements  YouTubePlayer.OnInitializedListener {
    private static final int RECOVERY_DIALOG_REQUEST = 10;
    public static final String API_KEY = "API KEY HERE";
    private String VIDEO_ID1 = "AeaFlRkWHrs";
                   String val;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teaching_videos);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
         YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_viewtraining);
        youTubeView.initialize(API_KEY, this);
    }
     private void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        if (!b) {
                 youTubePlayer.cueVideo(VIDEO_ID1);
    
         }
    }
    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError()) {
            youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("YouTube Error (%1$s)",
                    youTubeInitializationResult.toString());
            Toast.makeText(this, errorMessage, 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
            getYouTubePlayerProvider().initialize(API_KEY, this);
        }
    }
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }
}

 

 


Output

                   

Support us by Sharing