-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Streaming Youtube Videos with YouTubePlayerView
We can use the [YouTube Android Player API] (https://developers.google.com/youtube/android/player/) to play YouTube videos within an Android app.
In order to use this API, you will need to have Google Play installed on your emulator because the YouTube API interacts with a service that is distributed with the YouTube app. Either use a physical device to test or make sure you have followed this emulator setup guide to install Google Play services. Otherwise, you are likely to see An error has occurred while initializing the YouTube player
.
To start, you will need to create an API key through https://console.developers.google.com/. Make sure to enable the YouTube Data API v3
. Go to the Credentials
section and generate an API key.
Next, add the YouTubeAndroidPlayerApi.jar file into Android Studio by following this GIF to add this to your libs
dir. Once you've added the JAR, select Tools => Android => Sync project files with Gradle
in Android Studio to complete installation.
Instead of a VideoView
, you should add YouTubePlayerView
to your layout file where you'd like the video to be displayed:
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
If you intend for your Activity, you will need to extend YouTubeBaseActivity
. One current drawback is that this library does not inherit from AppCompatActivity
so some of your styles may not match those that are defined in styles.xml
.
public class QuickPlayActivity extends YouTubeBaseActivity {
You then should initialize the YouTube Player by calling initialize()
with your API key on the YouTubePlayerView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quick_play);
YouTubePlayerView youTubePlayerView =
(YouTubePlayerView) findViewById(R.id.player);
youTubePlayerView.initialize("YOUR API KEY",
new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
// do any work here to cue video, play video, etc.
youTubePlayer.cueVideo("5xVh-7ywKpE");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
If you wish to only load the video but not play, use cueVideo()
instead of loadVideo()
. Playing videos involves passing along the YouTube video key (do not include the full URL):
youTubePlayer.loadVideo("5xVh-7ywKpE");
You can review the working sample code here.
If you wish to force the video to landscape mode, you can also add the screenOrientation
flag inside your Activity in the AndroidManifest.xml file:
<activity
android:name=".QuickPlayActivity"
android:screenOrientation="landscape">
</activity>
Common issues with the YouTubePlayerView
are listed below:
-
Error DeadObjectException: If you receive the
java.lang.IllegalStateException: android.os.DeadObjectException
exception, you need to open up the "Play Store" and update the Youtube app on the Android device. - Error Leaked ServiceConnection: If you are using an emulator, upgrade to Lollipop (API 21) and open up the "Play Store" to check for updates to the Youtube app.
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.