Skip to content

Quick start

Pierfrancesco Soffritti edited this page Jun 30, 2018 · 24 revisions

The documentation has been moved in the readme of the project. This wiki is outdated.


This wiki explains how to use the Android-YouTube-Player library, feel free to contribute to make it better.

To get started using AndroidYouTubePlayer simply add the dependency to your module-level gradle file.

Download the library

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

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.

Ready to use example

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 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() {
                initializedYouTubePlayer.loadVideo(videoId, 0);
            }
        });
    }
}, true);

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

In this example I am loading a new video as soon as the player is ready. A more detailed explanation on how to use the library is given in the other pages of this wiki.