YouTube is the #1 video sharing platform with millions of users all over the globe. Integrate the power of YouTube in your application. Embedding YouTube videos in web applications is pretty straight forward. To embed youtube video in the android app needs little more effort. So in this tutorial, we will learn how to embed youtube videos in the android app.
DEMO
DOWNLOAD PROJECT
How To Embed YouTube video in android app
To Embed YouTube video in Android app we need to enable YouTube Data API and API key to access this service
STEP 1: Enable YouTube Data API:
Go to Google API Console Create new Project or choose existing.

Go to Enable API and search for YouTube Data API enable it.

STEP 2: Create API Key:
Go to the Credentials section and select Create Credentials -> API Key. Copy this key to clipboard we will need this while writing an Android app.

STEP 3: Adding YouTube Android Player API Library
Create a new android studio project or choose existing. Go to the YouTube Android Player API Library download page. Download and copy the YouTubeAndroidPlayerApi jar file from the archive paste it inside the libs folder of your project.

Then Right click on the jar file and select add as a library.

STEP 4: Create Activity
now create a new layout -> activity_main.xml which will contain YouTubePlayerView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.loopwiki.youtubeplayerexample.MainActivity"> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtubePlayerView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp"> <android.support.design.widget.TextInputEditText android:id="@+id/editTextId" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="Enter Youtube video Id" android:lines="1" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/buttonPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:text="Play" /> </LinearLayout>
create new class Package Name-> MainActivity.java
package com.loopwiki.youtubeplayerexample; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editTextId = findViewById(R.id.editTextId); Button buttonPlay = findViewById(R.id.buttonPlay); final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView); buttonPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String videoId = editTextId.getText().toString(); playVideo(videoId, youtubePlayerView); } }); } public void playVideo(final String videoId, YouTubePlayerView youTubePlayerView) { //initialize youtube player view youTubePlayerView.initialize("YOUR API KEY HERE", new YouTubePlayer.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.cueVideo(videoId); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }); } }
Breaking MainActvity
The following piece of code will set click listener on the play Button. When we will click on the play button it will take the id of video from EditText and pass it to method playVideo() Method.
buttonPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String videoId = editTextId.getText().toString(); playVideo(videoId, youtubePlayerView); } });
Inside playVideo() initialize YouTubePlayerView with API key. Then when onInitializationSuccess() call cueVideo() with id of video.
public void playVideo(final String videoId, YouTubePlayerView youTubePlayerView) { //initialize youtube player view youTubePlayerView.initialize("YOUR API KEY HERE", new YouTubePlayer.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.cueVideo(videoId); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }); }
Add internet permission inside your manifest.xml
<uses-permission android:name=”android.permission.INTERNET” />
Done now run the application.
If you still have any queries, please post them in the comments section below, I will be happy to help you.
14 Comments
error occurred while initializing youtube player ??
Very well explained thank you.
How can I change the Video quality ,because I am only getting 144p and 240p options
Awesome post! Thanks for helping me Mr Amardeep Yadav !! 🙂 🙂 🙂
Your welcome mate
I’m Cyril, My question this: I;ve built a tv app, but when i add youtube url of a live tv and other videos, instead of playing inside my app, it redirects me to youtube. Is there anything I can do to stop this?
yes i also need help on this
Does the you tube video have copyright issues while publishing our app in play store
May be or may be not you should check what others say about this.
Nice tutorial.Do you have any tutorial that is how to display all the videos of a specific youtube channel ?
That’s illegal i think.
yes i also need help on this
You can buy this codecanyon app.
Using this – You can show all videos of a specific channel, separated by Featured Playlist and Top video list.
link: https://codecanyon.net/item/android-youtube-video-app-hd-top-recent-genre-my-videos-search-videos/24510094
You can buy this codecanyon app.
Using this – You can show all videos of a specific channel, separated by Featured Playlist and Top video list.
link: https://codecanyon.net/item/android-youtube-video-app-hd-top-recent-genre-my-videos-search-videos/24510094