YouTube Integration with Android Studio
In todays world we are seeing lot of applications having video streaming or demo videos which will give instruction about how your application works. Storing video inside your project will increase apk size, instead of that we can store video on Youtube and stream it. Make a video with this editing software at makewebvideo.com/en/make/3d-text-intro-video, and then link your video, which’ll lead the user to Youtube.
Today we will learn how to stream youtube video in your android app.
first step we need to do is obtaining Google API Key, follow this steps to obtain Google API Key.
- Get SHA-1 fingerprint from your machine using java Keytool. Execute following command in your cmd/terminal.
On Windows
1 |
keytool -list -v -keystore "%USERPROFILE%.androiddebug.keystore" -alias androiddebugkey -storepass android -keypass android |
On Linux/MAC
1 |
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android |
- Go to Google Developer Console and create new project.
- Select Enable and manage APIs
- Search for YouTube Data API and enable it.
- Move to credentials from left sidebar and create new API key from Add credentials.
- From popup asking you for platform, select Android key.
- Enter key name<any name> and select Add package name and fingerprint.
- Enter your package name and sha-1 key we obtained earlier.
- click on create and you will get your API Key.
Now we have API Key . Let’s create a new project in Android Studio and start our first Youtube App.
- Create new project by navigating to File->New->New Project.
- Download latest version of Youtube Android API and find YouTubeAndroidPlayerApi.jar from extracted file.
- paste YouTubeAndroidPlayerApi.jar in your project libs folder.
- include jar file dependency in app level build.gradle file
12345dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:22.2.1'compile files('libs/YouTubeAndroidPlayerApi.jar')} - Create variables to store your API key and video id in your activity.
12private String API_KEY = "Your Obtained api_key";private String video_id = "NJiqKZxwDcM"; - Now add YouTubePlayerView in layout file.
1 - Now extend your MainActivity with YouTubeBaseActivity. Your MainActivity.java file will look like below :
123456789101112131415161718192021222324252627282930313233343536package com.youtubeapidemo;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.YouTubePlayerView;import youtubeapidemo.com.youtubeapidemo.R;public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{private String API_KEY = "Your Obtained API_key";private String video_id = "NJiqKZxwDcM";YouTubePlayerView youTubePlayerView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtube_view);youTubePlayerView.initialize(API_KEY, this);}@Overridepublic void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored){if (!wasRestored) {youTubePlayer.loadVideo(video_id);}}@Overridepublic void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult){Toast.makeText(MainActivity.this, youTubeInitializationResult.toString(), Toast.LENGTH_LONG).show();}}
- Finally add INTERNET permission in your manifest
1
Here it is, your first program of YouTubeVideoAPI.
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019