Before using the Android Youtube API, we need to register our application in the Google Developers Console. To register the application, follow these steps.
1. Go to the Google Developers Console
2. Create a new project.
3. From left slider, go to API/API Manager.
4. Click APIs. In the list of APIs, click on YouTube Data API and enable the Youtube Data API v3 on the page that appears.
5. In the left sidebar, select Credentials. Select API key from the Add Credentials dropdown menu. A popup will appear for you to specify the key type. Select Android Key. Next select Add package name and fingerprint and add the Android app's package name (add your package name here ie. com.neelam.videoyoutube) and then run the following command in Command prompt to get the SHA-1 certificate fingerprint.
keytool -list -v -keystore ~/.android/debug.keystore
Enter android as the password when prompted
Note: But before it you have to set path upto bin directory under java.
In the Terminal, we will see the MD5 and SHA-1 certificate fingerprints. Copy the SHA-1 fingerprint and paste it on the Google Developer Console and click on the Create button. An API key will be generated. We'll use this later.
Now come to the android project:
Create a class name Config.java and paste the following code there:
public class Config {
// Google Console APIs developer key
// Replace this key with your's
public static final String YOUTUBE_DEVELOPER_KEY = "AIzaSyCWAbCTOupVgfBAieR5NqAjUvIJ4lJ6FhA";
// YouTube video id
public static final String YOUTUBE_VIDEO_CODE = "fhWaJi1Hsfo";
}
Download the latest version of the YouTube Android Player API. Unzip the downloaded file to find the library jar file. Copy and paste it into your project's libs folder.
Modify activity_main.xml as shown.
package com.neelam.videoyoutube;
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.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubeStandalonePlayer;
public class MainActivity extends YouTubeBaseActivity {
private static final int RECOVERY_DIALOG_REQUEST = 1;
// YouTube player view
private YouTubePlayerView youTubeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, Config.YOUTUBE_DEVELOPER_KEY , Config.YOUTUBE_VIDEO_CODE);
startActivity(intent);
}
}
1. Go to the Google Developers Console
2. Create a new project.
3. From left slider, go to API/API Manager.
4. Click APIs. In the list of APIs, click on YouTube Data API and enable the Youtube Data API v3 on the page that appears.
5. In the left sidebar, select Credentials. Select API key from the Add Credentials dropdown menu. A popup will appear for you to specify the key type. Select Android Key. Next select Add package name and fingerprint and add the Android app's package name (add your package name here ie. com.neelam.videoyoutube) and then run the following command in Command prompt to get the SHA-1 certificate fingerprint.
keytool -list -v -keystore ~/.android/debug.keystore
Enter android as the password when prompted
Note: But before it you have to set path upto bin directory under java.
Now come to the android project:
Create a class name Config.java and paste the following code there:
public class Config {
// Google Console APIs developer key
// Replace this key with your's
public static final String YOUTUBE_DEVELOPER_KEY = "AIzaSyCWAbCTOupVgfBAieR5NqAjUvIJ4lJ6FhA";
// YouTube video id
public static final String YOUTUBE_VIDEO_CODE = "fhWaJi1Hsfo";
}
Modify activity_main.xml as shown.
<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"
tools:context=".MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Edit the strings.xml file as shown.<resources>
<string name="app_name">VideoYouTube</string>
<string name="hello_world">Hello world!</string>
<string name="player_error">Error initializing YouTube player</string>
</resources>
Modify MainActivity.java as shown.package com.neelam.videoyoutube;
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.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubeStandalonePlayer;
public class MainActivity extends YouTubeBaseActivity {
private static final int RECOVERY_DIALOG_REQUEST = 1;
// YouTube player view
private YouTubePlayerView youTubeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, Config.YOUTUBE_DEVELOPER_KEY , Config.YOUTUBE_VIDEO_CODE);
startActivity(intent);
}
}
Add Internet permission in your AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Now our app is ready to test.
Happy Coding...
Happy Coding...