Advanced Android - YouTube API



YouTube android API allows you to enable incorporate video playback functionality in your android application

Example

This example demostrate about how to integrate Android YouTube Android API.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout 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:paddingBottom = "@dimen/activity_vertical_margin"
   android:paddingLeft = "@dimen/activity_horizontal_margin"
   android:paddingRight = "@dimen/activity_horizontal_margin"
   android:paddingTop = "@dimen/activity_vertical_margin">
   <Button
      android:id = "@+id/button_play_youtube_video"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_centerHorizontal = "true"
      android:layout_centerVertical = "true"
      android:text = "Play YouTube Video" />
   <com.google.android.youtube.player.YouTubePlayerView
      android:id = "@+id/youtube_player_view"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_above = "@+id/button_play_youtube_video"
      android:layout_alignParentEnd = "true"
      android:layout_alignParentLeft = "true"
      android:layout_alignParentRight = "true"
      android:layout_alignParentStart = "true"
      android:layout_alignParentTop = "true" />
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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.YouTubePlayerView;

public class MainActivity extends YouTubeBaseActivity {
   Button mbutton;
   private YouTubePlayerView myouTubePlayerView;
   private YouTubePlayer.OnInitializedListener monInitializedListener;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      myouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);
      monInitializedListener = new YouTubePlayer.OnInitializedListener() {
         @Override
         public void onInitializationSuccess(
            YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            
            youTubePlayer.loadVideo("MbYJjJL65hw");
         } 
         
         @Override
         public void onInitializationFailure(YouTubePlayer.Provider provider, 
            YouTubeInitializationResult youTubeInitializationResult) {
         } 
      }; 
      mbutton = (Button) findViewById(R.id.button_play_youtube_video);
      mbutton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            myouTubePlayerView.initialize("AIzaSyA_bMEqSJbYilFoPeVwrxtGhKfGWfs22_g", 
               monInitializedListener);
         }
      });
   }
}

Step 4 − Add the following code to build.gradle

dependencies {
   compile fileTree(include: ['*.jar'], dir: 'libs')
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:24.2.1'
   compile 'com.google.android:flexbox:0.1.3'
   compile files('libs/YouTubeAndroidPlayerApi.jar')
}

Step 5 − Add the jar file to lib.

Download the jar file at here

Step 6 − Add the following code to manifest.xml

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
   package = "myapplication.example.com.myapplication">
   <uses-permission android:name = "android.permission.INTERNET"/>
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Youtube
Advertisements