Like Us on Facebook

How to play youtube video in Android Application

Hi , In this  tutorial I am going to explain  How to play  youtube  video in any Android application. We are  going to use  youtube  API to play  youtube video within a Android application. Youtube API  required minimum  Android version 4.2 .We can see lots of  android apps playing  youtube video inside the App to tell the app intro . This app will have a single screen with a video playing in it. This article covers basics of YouTube Android API. If you want to build a fully  fledged  youtube  app, please go through  Youtube Android Player API docs provided by  Google.

Read more 
How to play Video streaming Application in Android
How  to parse Json Data in Android Application
How to Create PDF reader Application in Android 
How to create  your  first Android Game

Prerequisites:

  • JDK 7.0 or Above
  • Android Studio 2.0
 

Steps to follow:

Step 1: Create a New Android Studio project name YoutubePlayer.

Step 2: Select minimum API level 17 so  that it can run Maximum of  the Android device  available in playstore.

Step 3: Give your layout xml name is activity_main And click finish .

Step 4:  Login to API console

Step 5:  Create a Project -> Go to Services -> Turn on the switch for Google YouTube Data API v3

Step 6:  Goto API Access -> Create new Android Key-> In popup enter one SHA1 certificate fingerprint and package name (separated by a semicolon) per line.

Step 7:  To get SHA1 certificate of your app in Android studio open Terminal enter the following  command keytool -list -v –keystore “path to key” . Then  enter the password of your keystore . Scroll above to get SHA1  fingerprint   key .

Step 8: From here copy SHA1 certificate and also copy package name of your Apps from Android Studio.

Step 9: Now enter these values in the API Console popup in the desired format, you key will be generated.

Step 10: Now Download Android YouTube Player API bundle from here. Unzip this file and follow the steps below to include it in your app:
  1. Copy the jar file from libs folder to the the libs folder of your project.
  2. Right click on your project, File -> Project Structure -> app -> dependencies ->  add File dependencies
  3.  Select YouTubeAndroidPlayerAPI.jar ->Ok
 
Step 11 : in manifest file add the  internet permission
 
<uses-permission android:name="android.permission.INTERNET"/>
 
Step 12: Inside the  activity_main.xml write the following code
 
<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:id ="@+id/play"
 tools:context="com.example.prosen.youtubeplayer.MainActivity">
 
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!"
 android:id="@+id/textView" />
 
<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Play"
 android:id="@+id/button1"
 android:layout_centerVertical="true"
 android:layout_centerHorizontal="true" />
 
<com.google.android.youtube.player.YouTubePlayerView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/view1"
 android:layout_alignParentRight="true"
 android:layout_alignParentEnd="true"
 android:layout_above="@+id/button1"
 android:layout_alignParentLeft="true"
 android:layout_alignParentStart="true"
 android:layout_alignParentTop="true" />

</RelativeLayout>
 
 Step 13 : Inside MainActivity.java  write the following code
 
 import android.support.v7.app.AppCompatActivity;
 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 button;
 private YouTubePlayerView view;
 private YouTubePlayer.OnInitializedListener listener;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 view = (YouTubePlayerView)findViewById(R.id.view1)

 listener = new YouTubePlayer.OnInitializedListener() {
 @Override
 public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer  youTubePlayer, boolean b) {
 
 youTubePlayer.loadVideo("BvpqMiVNQ7s");
 
  }
 
 @Override
 public void onInitializationFailure(YouTubePlayer.Provider provider,  YouTubeInitializationResult youTubeInitializationResult) {
 button =(Button)findViewById(R.id.button1);
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 view.initialize("AIzaSyAi_AF4xeTeo8Q_S0-3j0PFZWWLsycfzMs",listener);
 }
 });
 }
 };
 }
}


Now compile and test the Application in real device.

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also