xitorrito/Android-YouTube-Player


Alternative YouTube player for Android.

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.xitorrito:Android-YouTube-Player:2.1.2'
	}
	dependencies {
		implementation("com.github.xitorrito:Android-YouTube-Player:2.1.2")
	}
	<dependency>
	    <groupId>com.github.xitorrito</groupId>
	    <artifactId>Android-YouTube-Player</artifactId>
	    <version>2.1.2</version>
	</dependency>

                            
    libraryDependencies += "com.github.xitorrito" % "Android-YouTube-Player" % "2.1.2"
        
        

                            
    :dependencies [[com.github.xitorrito/Android-YouTube-Player "2.1.2"]]
        
        

Readme


Android-YouTube-Player

Android Arsenal

The AndroidYouTubePlayer is 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.

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. I've tried to use it but my app was crashing because of internal bugs in Google's player, (sone bugs have 3+ years old bug reports) and, at the moment, no update has been released for almost a year. This library provides a stable and open source alternative to the official YouTube Player.

Apps using this library: (send me an email if you want to add your app to the list)

<img height="450" src="https://github.com/PierfrancescoSoffritti/AndroidYouTubePlayer/blob/master/pics/ayp.gif" />

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:2.0.0'
}

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

The second parameter of the initialize method is a boolean, set it to true if you want the YouTubePlayerView to handle network events, if you set it to false you should handle network events with your own broadcast receiver.

The AbstractYouTubePlayerListener is just a convenience abstract class implementing YouTubePlayerListener, so that is not necessary to implement all the methods of the interface.

The playback of the videos is handled by the YouTubePlayer. You must use that for everything concerning the 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();

Use the methods uiController.setCustomAction1 and uiController.setCustomAction2 to add/remove custom actions at the left and right of the play/pause button.

uiController.setCustomAction1(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_pause_36dp), new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    }
});