How to embalmed YouTube video in your app with android studio .How to Play YouTube Video in Android Using GitHub Library?
If you want play YouTube video in your android app then in this article i will explain you , how you can play YouTube video inside your app with a library .
Follow these steps -
Step 1 - Create A New Project And Add Internet Permission in your manifest file AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Step 2 - Add bellow given library dependency in your build.gradle file
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:chromecast-sender:0.23'
Library dependency link - click
Step 3 - Design your .xml layout
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="@+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:showSeekBar="true"
app:autoPlay="false"
app:showVideoDuration="true"
app:showVideoCurrentTime="true"
app:showYouTubeButton="false"
app:showFullScreenButton="false" />
</LinearLayout>
</RelativeLayout>
Step 4 - Write code in your java activity
before onCreate Method declare the id of which video you want to play
String video_id ="jNQXAC9IVRw";
in the onCreate method Write these lines of codes
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youTubePlayerView);
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
String videoId = video_id;
youTubePlayer.loadVideo(videoId, 0);
}
});
how to copy YouTube id