We using Youtube Android API and must be activated. Then we create the credentials. For more information please go through Youtube Android Player API.
See also how to login using facebook
ACTIVATED THE YOUTUBE API KEY AND GET THE CREDENTIAL
1. Go to Developer Console
2. Create New Project or use an existing project
![]() |
Create project |
3. If you choose to create new project, then fill your project name
![]() |
Fill the project name |
4. Take a look to API Youtube, and click Youtube Data API
![]() |
Youtube data API |
5. Enable the Youtube Data API
![]() |
Enable |
6. Create credential by clicked the button Create Credential
![]() |
Create the credential |
7. Fill the credential field
![]() |
Credential field |
9. Here it is, the credentials
![]() |
The credentials |
That's all the steps how to enable the Youtube API and create the credentials on your project. Now time to start the Android project.
CREATE ANDROID STUDIO PROJECT
Download the latest version of Youtube Android Player API, HERE. Extract the zip file and find the YoutubeAndroidPlayerApi.jar.
Place the YoutubeAndroidPlayerApi.jar in your project's libs folder. Right click on the jar, choose menu Add As Library.
Create one layout :
- activity_main.xml
Create two java classes :
- Constant.java
- MainActivity.java
After you creating all layout and java classes, now modify these layout and java classes one by one.
Modify your activity_main.xml, paste the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
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="com.putuguna.youtubeplayeronapps.MainActivity"> | |
<TextView | |
android:textStyle="bold" | |
android:gravity="center" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Subscribe : url/channel/UCt7a2o143ngbccCuV0ZMrpQ" | |
android:layout_marginBottom="10dp"/> | |
<com.google.android.youtube.player.YouTubePlayerView | |
android:id="@+id/youtube_player" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"></com.google.android.youtube.player.YouTubePlayerView> | |
</LinearLayout> |
Example the URL is : https://www.youtube.com/watch?v=WhRaCA9bC9k. WhRaCA9bC9k is the video ID.
take a look at the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.youtubeplayeronapps; | |
/** | |
* Created by putuguna on 09/12/16. | |
*/ | |
public class Constant { | |
public static final String YOUTUBE_API_KEY = "AIzaSyCdPy5ZUkJ1arINjSEHmGx0SgrjlctOTwU"; | |
public static final String YOUTUBE_ID = "WhRaCA9bC9k"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.youtubeplayeronapps; | |
import android.content.ContentResolver; | |
import android.content.ContentValues; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.provider.Settings; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
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 MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { | |
private static final int REQUEST_NUMBER = 999; | |
private YouTubePlayerView youTubePlayerView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player); | |
//set the api key here | |
youTubePlayerView.initialize(Constant.YOUTUBE_API_KEY,this); | |
} | |
@Override | |
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { | |
if(!b){ | |
youTubePlayer.cueVideo(Constant.YOUTUBE_ID); | |
/** | |
* there are 2 method you can user here : | |
* .cueVideo(), for didn't play automatically | |
* .loadVideo(), for do play automatically | |
* | |
* if you are using play automatically, it better if you hide the video controllers | |
* do like below : | |
* youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);//hide the players controllers | |
*/ | |
} | |
} | |
@Override | |
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { | |
if(youTubeInitializationResult.isUserRecoverableError()){ | |
youTubeInitializationResult.getErrorDialog(this, REQUEST_NUMBER).show(); | |
}else{ | |
String errorMessage = String.format( | |
"There was an error initializing the YouTubePlayer (%1$s)", youTubeInitializationResult.toString() | |
); | |
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
//super.onActivityResult(requestCode, resultCode, data); | |
if(requestCode==REQUEST_NUMBER){ | |
youTubePlayerView.initialize(Constant.YOUTUBE_API_KEY,this); | |
} | |
} | |
} |
<uses-permission android:name="android.permission.INTERNET"/>
DOWNLOAD THE PROJECT HERE : GITHUB LINK
EmoticonEmoticon