android-youtube-player

Additional

Language
Kotlin
Version
10.0.4 (Aug 25, 2019)
Created
Aug 29, 2016
Updated
Aug 25, 2019
Owner
Pierfrancesco Soffritti (PierfrancescoSoffritti)
Contributors
talklittle
Matt Kula (matthewrkula)
Askar (askarsyzdykov)
Pierfrancesco Soffritti (PierfrancescoSoffritti)
Denino
RmK (rajeefmk)
jguerinet
Apsaliya
ambiguous1991
ValCanBuild
CajetanP
ct7ct7ct7
solt9029
hannojg
kpiskur
15
Activity
Badge
Generate
Download
Source code
APK file

Advertising

android-youtube-player

android-youtube-player is a stable and customizable open source YouTube player for Android. It provides a simple View that can be easily integrated in every Activity/Fragment.

To interact with YouTube the library uses the IFrame Player API, inside of a WebView, therefore the YouTube app is not required on the user's device and there are no issues with YouTube Terms of Service.

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.

The UI of the player is 100% customizable. The default UI can be changed, to show and hide new views, or can be completely replaced by a custom UI.

This library also provides a Chromecast YouTube player, that you can use to cast YouTube videos from your app to a Chromecast device.

Why does this library exist?

The official library provided by Google to integrate YouTube videos in Android apps is the YouTube Android Player API. The official library is quite buggy (some bugs are 5+ years old) and lacks in support from Google. I found it to be quite unreliable and therefore unusable in production.

This, added to its limited options for customization and lack of Chromecast support, lead me to the development of this open source library.

A lengthier explanation to why you may want to consider using an alternative to the official YouTube player is written in this Medium post.

A list of published apps that are using this library: (let me know if you want to add your app to this list)


It is recommended that you add YouTubePlayerView as a lifecycle observer of its parent Activity/Fragment. You can read why in the documentation.

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youTubePlayerView);

(If you have problems adding YouTubePlayerView as a LifecycleObserver, you probably aren't using androidx, I suggest you migrate your dependencies)

That's all you need, a YouTube video is now playing in your app.

If you want more control, everything can be done programmatically by getting a reference to your YouTubePlayerView and adding a YouTubePlayerListener to it.

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youTubePlayerView);

youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
  @Override
  public void onReady(@NonNull YouTubePlayer youTubePlayer) {
    String videoId = "S0Q4gqBUs7c";
    youTubePlayer.loadVideo(videoId, 0);
  }
});