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

How To Embed YouTube video in android app | loopwiki.com

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.

Embed YouTube video in android - Create Developer Console Project

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

Embed YouTube video in android - Enable YouTube Data API

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.

Embed YouTube video in android - Create API Key

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.

Embed YouTube video in android - Paste Library in Libs Folder

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

Embed YouTube video in android - Add as 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) {

                    }
                });
    }


}

Embed YouTube video in android - Output How to Embed YouTube video in android

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.

Author

Hello there, My name is Amardeep founder of loopwiki.com. I have experience in many technologies like Android, Java, Php, etc. In this variety of technologies, I love Android App Development. If you have any idea and you want me to develop for you then let's have chat Conatct

13 Comments

  1. How can I change the Video quality ,because I am only getting 144p and 240p options

  2. K P Ranjith Kumar Reply

    Awesome post! Thanks for helping me Mr Amardeep Yadav !! 🙂 🙂 🙂

  3. Tochukwu Cyril Reply

    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?

  4. Does the you tube video have copyright issues while publishing our app in play store

  5. Nice tutorial.Do you have any tutorial that is how to display all the videos of a specific youtube channel ?

Write A Comment