How to play YouTube video in app using YouTube API V3

 

We can play YouTube video using WebView but this method is not working well because our app got high space on RAM that was the reason of developing YouTube API.

API has several versions but we can use V3 this version is latest and it has a lot of new feature like we can play video horizontally and we can play without using listener.

Steps

First step is to create a google account. Everyone know that how to create a google account so I cannot explain this.

Second step is Download Youtube api from developer website. Link

Third step is click on android and then select project.

Fourth step is to paste that library in libs folder.

Fifth Step is to add dependency and then click on sync.

Six Step is to enable api from google developer and then create api.

Last Step is to add api in java code. Please follow my steps video is below the activity_main.xml.

Code

Build.gradule

implementation files('libs\\YouTubeAndroidPlayerApi.jar')

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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
,YouTubePlayer.PlaybackEventListener,YouTubePlayer.PlayerStateChangeListener
{

    YouTubePlayerView youTubePlayerView;
    String api_key="paste api here";
    String video_id="gFQmOMZEBj8";// this is video api which is play


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        youTubePlayerView=(YouTubePlayerView )findViewById ( R.id.youtubePlayer );
        youTubePlayerView.initialize ( api_key,this );

    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

        youTubePlayer.setPlayerStateChangeListener ( this );
        youTubePlayer.setPlaybackEventListener ( this );

        if(!b)
        {
            youTubePlayer.cueVideo ( video_id );

        }

    }

    @Override
    public void onPlaying() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void onStopped() {

    }

    @Override
    public void onBuffering(boolean b) {

    }

    @Override
    public void onSeekTo(int i) {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded(String s) {

    }

    @Override
    public void onAdStarted() {

    }

    @Override
    public void onVideoStarted() {

    }

    @Override
    public void onVideoEnded() {

    }

    @Override
    public void onError(YouTubePlayer.ErrorReason errorReason) {

    }
}

 

Activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:id="@+id/youtubePlayer"
    android:background="#fff"
    > </com.google.android.youtube.player.YouTubePlayerView>
</RelativeLayout>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *