android-youtube-player, an open source alternative to the official YouTube Player API
a short introduction to the android-youtube-player API and why you may want to use it instead of the official YouTube Android Player API
You’re building your new Android app and you want to enable your users to watch YouTube videos in it. The first thing you may think of is to use the official YouTube Android Player API. This is not necessarily the best choice. With this post I’m going to introduce an alternative open source YouTube player I have built (android-youtube-player), explaining some of the benefits you may get using it instead of the official player.
Why you should consider not using the official library from YouTube
As of May 1 2023, the Official Android YouTube Player is deprecated. Which makes android-youtube-player
the number one YouTube Player for Android, used by over 5 thousands apps.
The following reasons are at this point historical reasons.
If you’re concerned about stability
The library has some very old major bugs, like this one. While developing my app I kept running into it, causing my app to randomly crash for apparently no reason, making it unreliable in production.
If you don’t want to be tied to Google Play and the YouTube app
The official Player API requires Google Play and the YouTube app to be installed on the user’s device.
If you want more control over the player looks and behavior
The official Player API doesn’t allow for any customization of the UI.
Maybe you want to change the UI of the player or write some custom behavior specific for your use case. That is not possible with the official player.
From all of these, the biggest concern I had was with the stability of the library. Maybe someday Google will release a new version that works, but that has yet to happen (since 2013 :|).
The alternative: android-youtube-player API
The android-youtube-player API provides a simple View that can be easily integrated in every Activity and Fragment.
Under the hood the library interacts with YouTube using the IFrame Player API, that runs in a WebView.
If you want to use the web UI of the web player, you can do it. But you can also use the customizable native UI provided by the library. You can even build your own custom UI from scratch.
Since the API use the official IFrame Player API to access YouTube, there are no issues with terms of service violations.
Ease of use
android-youtube-player doesn’t require its users to extend annoying Activities or Fragments, it is just a View. You can drop it wherever you want.
Customizable UI
android-youtube-player gives a lot of freedom in terms of UI customization. If you want, you can completely replace the player’s UI with your own.
Or you can easily add/remove Views from the default UI.
Chromecast support
Starting from version 8 the android-youtube-player API can also be used to cast videos from an Android app to a Google Cast device.
Quick start — use the library with just a few lines of code
I’m now going to show a simple example of how to get up and running with the library, with just a few lines of code.
The source code of a few other examples is available here, in the sample app. You can also download the sample app apk and try it on your phone.
First steps
To use the API start by importing the library dependency, by adding this line to your module-level build.gradle
.
(replace “last_version” with actual last version number of the library. Released versions can be found here)
In order to start using the player you need to add aYouTubePlayerView
to your layout. You can also create it programmatically, if you prefer.
In your Activity/Fragment, get a reference to the YouTubePlayerView
and add it as an observer to the Activity/Fragment’s Lifecycle.
This is all you need to start playing YouTube videos 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.
There is no easier way to play YouTube videos on Android.
Next steps
Where can you find me?
PlayStoreReply: https://playstorereply.com
Follow me on Twitter: https://twitter.com/psoffritti
My website/portfolio: pierfrancescosoffritti.com
My GitHub account: https://github.com/PierfrancescoSoffritti
My LinkedIn account: linkedin.com/in/pierfrancescosoffritti/en