Table of Contents
- Downloading YouTubePlayer API
- Register your app in Google Developer Console
- Create a new Android application project
- Define your activity layout
- Initialize YouTubePlayerView in your acitvity
- Output
- Reference
In this tutorial we'll see how to use YouTubePlayer API in Android. YouTube player API provides an embedded view that supports with various playback controls.
1. Downloading YouTubePlayer API
Before you start writing some code, you'll have to first download the YouTube Android Player API. Once downloaded import the jar into your project lib folder and add the jar to your build path.
Download the YouTubePlayer API here
2. Register your app in Google Developer Console
- Before you start using the Youtube player API for Android, you have to register your application with Google Developer Console and get the API key. The API key is a unique value for your application. Do not disclose it to others.
- Visit Google APIs Console and login with your Google account and create a new project.
- Once your project is created, go to API's and enable "YouTube data API V3"
- And the select "API Access" in your API Console. You'll find API key. This API key will be used to access YouTube Android Player APIs.
3. Create a new Android Application project
Now you are ready to create a new Android project to play Youtube videos using YouTube player API. You may use IDE of your choice, here I am using Eclipse.
You need INTERNET permission to play video over internet. Add below permission in your application AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
4. Define your activity layout
In this example, we will create an sample layout that uses YouTubePlayerView inside an LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtube_player" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:padding="5dp" /> </LinearLayout>
5. Initialize YouTubePlayerView in you Activity
Initializing YouTubePlayerView by calling initialize method. Once the initialize in successful, you will get the onInitializationSuccess() callback with YouTubePlayer instance, You can add additional listenersto handle the playback controls.
package com.example.youtubeplayer; import android.os.Bundle; import android.widget.Toast; import com.google.android.youtube.player.YouTubeBaseActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayer.ErrorReason; import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener; import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener; import com.google.android.youtube.player.YouTubePlayer.Provider; import com.google.android.youtube.player.YouTubePlayerView; import com.javatechig.youtubeandroid.R; public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { public static final String API_KEY = "YOUR API KEY"; //http://youtu.be/<VIDEO_ID> public static final String VIDEO_ID = "dKLftgvYsVU"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** attaching layout xml **/ setContentView(R.layout.activity_main); /** Initializing YouTube player view **/ YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player); youTubePlayerView.initialize(API_KEY, this); } @Override public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) { Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show(); } @Override public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) { /** add listeners to YouTubePlayer instance **/ player.setPlayerStateChangeListener(playerStateChangeListener); player.setPlaybackEventListener(playbackEventListener); /** Start buffering **/ if (!wasRestored) { player.cueVideo(VIDEO_ID); } } private PlaybackEventListener playbackEventListener = new PlaybackEventListener() { @Override public void onBuffering(boolean arg0) { } @Override public void onPaused() { } @Override public void onPlaying() { } @Override public void onSeekTo(int arg0) { } @Override public void onStopped() { } }; private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() { @Override public void onAdStarted() { } @Override public void onError(ErrorReason arg0) { } @Override public void onLoaded(String arg0) { } @Override public void onLoading() { } @Override public void onVideoEnded() { } @Override public void onVideoStarted() { } }; }
6. Output
7. References
how change url video ?
ReplyDelete