Android-YouTube-Player

Additional

Language
Java
Version
4.1.4 (Jan 18, 2018)
Created
Aug 29, 2016
Updated
Jan 18, 2018
Owner
Pierfrancesco Soffritti (PierfrancescoSoffritti)
Contributors
talklittle
Askar (askarsyzdykov)
Pierfrancesco Soffritti (PierfrancescoSoffritti)
Denino
4
Activity
Badge
Generate
Download
Source code
APK file

Advertising

Android-YouTube-Player

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 use this player. 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.

Why does this library exists?

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 and the total lack of support from Google made it impossible to use in production. Originally I have tried to use the official library, but my app was crashing because of internal bugs in Google's player, ( some bugs have 3+ years old bug reports) those bugs haven't been fixed for years. From there I decided to build my own player.

More reasons why you may want to consider using an alternative to the official YouTube player are written in this 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)

Download

Add this to your project-level build.gradle:

allprojects {
  repositories {
    ...
    maven { url "https://jitpack.io" }
  }
}

Add this to your module-level build.gradle:

dependencies {
  compile 'com.github.PierfrancescoSoffritti:AndroidYouTubePlayer:4.1.3'
}

Proguard

If you are using ProGuard you might need to add the following option:

-keep public class com.pierfrancescosoffritti.youtubeplayer.** {
   public *;
}

-keepnames class com.pierfrancescosoffritti.youtubeplayer.*

Usage

A sample project that shows how to use the library is available in the sample module. You can also download the sample apk here.

Please refer to the Wiki of the library for a detailed description on how to use it.

Quick start

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();