How to play YouTube video in Android app

In this post, I am going to show you how you can create an android app, that will play YouTube Video. Our app will have a simple play button. Clicking the play button will play a youtube video. We will be using this video from Youtube for our case : https://www.youtube.com/watch?v=ZENmkJk9fBM.


Create a new project. Lets name it YouTubeDemoApp.


Select the target device:



Select 'Empty Activity'


Let the main activity name be 'MainActivity' and the layout file be 'activity_main'.



Now, download the YouTubeAndroidPlayer library from https://developers.google.com/youtube/android/player/downloads/. Extract the zip file in libs folder you will find YouTubeAndroidPlayer.jar file. Add the YouTubeAndroidPlayerApi.jar file to libs folder in the app module of the project. Sync project with gradle file.

Now lets create a button in activity_main.xml file. Here is my code for the activity_main.xml:

<relativelayout 
    android:layout_height="match_parent" 
    android:layout_width="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" 
    tools:context="com.tuxkiddos.apps.youtubedemoapp.MainActivity" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools">

    <button 
         android:id="@+id/playBtn" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="@string/play">
    </button>
</relativelayout>


In your strings.xml file add this:

<string name="play">Play</string>

In the MainActivity file, we will setup OnClickListener for the button to play a YouTube Video. The code is as follows:

package com.tuxkiddos.apps.youtubedemoapp;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final int REQ_START_STANDALONE_PLAYER = 1;
    private static final int REQ_RESOLVE_SERVICE_MISSING = 2;
    private static final String DEVELOPER_KEY = "******************************";
    private String youtubeVideoId = "ZENmkJk9fBM";

    private Button playBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playBtn = (Button)findViewById(R.id.playBtn);
        playBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = YouTubeStandalonePlayer.createVideoIntent(MainActivity.this,DEVELOPER_KEY,
                        youtubeVideoId, 0, true, false);
                if (intent != null) {
                    if (canResolveIntent(intent)) {
                        startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
                    } else {
                        // Could not resolve the intent - must need to install or update the YouTube API service.
                        YouTubeInitializationResult.SERVICE_MISSING
                                .getErrorDialog(MainActivity.this, REQ_RESOLVE_SERVICE_MISSING).show();
                    }
                }
            }
            private boolean canResolveIntent(Intent intent) {
                List resolveInfo = getPackageManager().queryIntentActivities(intent, 0);
                return resolveInfo != null && !resolveInfo.isEmpty();
            }
        });
    }
}


You should use your own Developer Key to run the app. You can register for a developer key at Google developer console. Instructions are at : https://developers.google.com/youtube/android/player/register. Once you have replaced with the developer key now run the app.

Comments

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud

Bruteforce a router to hack admin password using Hydra