The Android-YouTube-Player library provides a simple View that can be easily integrated in every Activity/Fragment.
The interaction with YouTube is based on the IFrame Player API, running inside of a WebView, therefore the YouTube app is not required to be installed on the user's device.
The web UI of the IFrame Player player is hidden, instead a native UI built on top of Android is used to interact with the player, providing a native experience to the users.
This library has been developed out of necessity. The official library provided by Google for the integration of YouTube videos into Android apps is the YouTube Android Player API. Its many bugs (some are 5+ years old) and the lack of support from Google made it impossible to use in production.
A lengthier explanation to why you may want to consider using an alternative to the official YouTube player is written in this Medium post.
This library has a Wiki, check it out!
A list of published apps that are using this library: (let me know if you want to add your app to this list)
Add this to your project-level build.gradle
:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add this to your module-level build.gradle
:
dependencies {
implementation 'com.github.PierfrancescoSoffritti:AndroidYouTubePlayer:6.0.0'
}
If you are using ProGuard you might need to add the following options:
-keep public class com.pierfrancescosoffritti.youtubeplayer.** {
public *;
}
-keepnames class com.pierfrancescosoffritti.youtubeplayer.*
A sample project that shows how to use the library is available in the sample module. You can also download the sample apk here, or on the PlayStore.
Please refer to the Wiki of the library for a detailed description on how to use it.
In order to start using the player you need to add the YouTubePlayerView to your layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerView
android:id="@+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Get a reference to the YouTubePlayerView
in your code and initialize it
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(new YouTubePlayerInitListener() {
@Override
public void onInitSuccess(final YouTubePlayer initializedYouTubePlayer) {
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady() {
String videoId = "6JYIGclVQdw";
initializedYouTubePlayer.loadVideo(videoId, 0);
}
});
}
}, true);
More info on the initialization method can be found here.
The AbstractYouTubePlayerListener is just a convenience abstract class that implements YouTubePlayerListener
, so that is not necessary to always implement all the methods of the interface.
The playback of the videos is handled by the YouTubePlayer. You must use that for everything concerning video playback.
The UI of the player is handled by a PlayerUIController, in order to interact with it you must get its reference from the YouTubePlayerView
PlayerUIController uiController = youTubePlayerView.getPlayerUIController();
You can use the PlayerUIController
to customize the UI of the player. You can show or hide Views in it, add your own Views and icons, remap click listeners etc. Read more about it here.
If you need even more control over the UI and you want to design it specifically for your app, you have the freedom to do it. You can completly replace the default UI with your own. Simply call the method View YouTubePlayerView.inflateCustomPlayerUI(@LayoutRes int customPlayerUILayoutID)
.
This method takes in the id of a layout resource. The method returns the View object corresponding to the inflated layout. The default UI of the player gets removed and replaced with the new UI, giving you all the freedom you want. You can read more in the Wiki and in this blog post.