Youtube Player in android

Introduction

    The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.
    Using the API, you can load or cue videos into a player view embedded in your application's UI. You can then control playback programmatically. For example, you can play, pause, or seek to a specific point in the currently loaded video.

Before you need to do this steps for API access Register your app using YouTube Android Player API and obtain API Key

Download this YouTube Android Player API and added into your library

demo


Using the code

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6E6E6E"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/top_title_layout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#81BEF7"
android:orientation="vertical" >

<TextView
android:id="@+id/title_Text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="YOUTUBE VIDEO"
android:textColor="#DF01D7"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/top_title_layout"
android:orientation="vertical"
android:weightSum="2" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.20"
android:orientation="vertical"
android:padding="5dp" >

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".80"
android:background="#A9E2F3"
android:orientation="vertical" >

<ScrollView
android:id="@+id/Document_ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#FFFFFF"
android:fillViewport="true"
android:padding="5dp" >

<LinearLayout
android:id="@+id/MiddleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >

<TextView
android:id="@+id/videodesc_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tamil period-drama Kochadaiiyaan, starring superstar Rajinikanth, has been made at a budget of Rs.125 crore, says its producer J Murali Manohar. He also says that his film should not be compared with Avatar or Tin Tin because they were made at huge budgets." />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>

</RelativeLayout>

MainActivity.java:
package com.example.youtubevideoplayer;

import com.example.youtubevideoinwebview.R;
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.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{

// <-- Create new project in Google Developers Console.-->
// <-- https://developers.google.com/youtube/android/player/register -->
// <-- Its similar to GoogleMaps Registrtion -->
public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";

// <-- Get Video ID Last 11 digit numbers for youtube video Url -->
// <-- For Example https://www.youtube.com/watch?v=HQDZq5val0I -->
public static final String VIDEO_ID = "HQDZq5val0I";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEYthis);
}

@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(),
"onInitializationFailure()",
Toast.LENGTH_LONG).show();
}

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}

}             

                                                   
Previous
Next Post »

3 comments

Write comments
Unknown
AUTHOR
November 24, 2014 at 6:17 PM delete

YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);\

i am getting error here what am do now........

Reply
avatar
Kalidoss
AUTHOR
November 27, 2014 at 11:18 AM delete

Add youtube library in your project properly then import.

Reply
avatar