Widget Recent Post No.

header ads

How to integrate Youtube API Library in Android Studio


 Step 1 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"

    android:paddingBottom="20dp"

    android:paddingLeft="0dp"

    android:paddingRight="0dp"

    android:paddingTop="0dp"

    tools:context=".MainActivity">


    <Button

        android:background="@drawable/btn"

        android:id="@+id/button_play_youtube_video"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:textColor="#FFF"

        android:text="Play YouTube Video" />


    <com.google.android.youtube.player.YouTubePlayerView

        android:id="@+id/youtube_player_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_above="@+id/button_play_youtube_video"

        android:layout_alignParentEnd="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentStart="true"

        android:layout_alignParentTop="true" />

</RelativeLayout>


Step 2 main.java

package in.learncodewithrk.youtubevideo;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;

import android.view.View;

import android.widget.Button;


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 {


    Button mbutton;

    private YouTubePlayerView myouTubePlayerView;

    private YouTubePlayer.OnInitializedListener monInitializedListener;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);

        monInitializedListener = new YouTubePlayer.OnInitializedListener() {

            @Override

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

                youTubePlayer.loadVideo("fpPuK88J_yU");

            }


            @Override

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


            }

        };


        mbutton = (Button) findViewById(R.id.button_play_youtube_video);

        mbutton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                myouTubePlayerView.initialize("AIzaSyBuprTAJrBDUVcDXtJzawZr6RdPtbkOcaI", monInitializedListener);

            }

        });

    }

}


GITHUB : https://github.com/LearncodeWithRk/youtubePlayerAPI

Post a Comment

0 Comments