Can I Embed A Youtube Video In My App
YouTube is the #1 video sharing platform with millions of users all over the globe. Integrate the power of YouTube in your application. Embedding YouTube videos in web applications is pretty straight forward. To embed youtube video in the android app needs little more effort. So in this tutorial, we will learn how to embed youtube videos in the android app.
DEMO
DOWNLOAD PROJECT
How To Embed YouTube video in android app
To Embed YouTube video in Android app we need to enable YouTube Data API and API key to access this service
STEP 1: Enable YouTube Data API:
Go to Google API Console Create new Project or choose existing.
Go to Enable API and search for YouTube Data API enable it.
STEP 2: Create API Key:
Go to the Credentials section and select Create Credentials -> API Key. Copy this key to clipboard we will need this while writing an Android app.
STEP 3: Adding YouTube Android Player API Library
Create a new android studio project or choose existing. Go to the YouTube Android Player API Library download page. Download and copy the YouTubeAndroidPlayerApi jar file from the archive paste it inside the libs folder of your project.
Then Right click on the jar file and select add as a library.
STEP 4: Create Activity
now create a new layout -> activity_main.xml which will contain YouTubePlayerView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.loopwiki.youtubeplayerexample.MainActivity"> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtubePlayerView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp"> <android.support.design.widget.TextInputEditText android:id="@+id/editTextId" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="Enter Youtube video Id" android:lines="1" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/buttonPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:text="Play" /> </LinearLayout>
create new class Package Name-> MainActivity.java
package com.loopwiki.youtubeplayerexample; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editTextId = findViewById(R.id.editTextId); Button buttonPlay = findViewById(R.id.buttonPlay); final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView); buttonPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String videoId = editTextId.getText().toString(); playVideo(videoId, youtubePlayerView); } }); } public void playVideo(final String videoId, YouTubePlayerView youTubePlayerView) { //initialize youtube player view youTubePlayerView.initialize("YOUR API KEY HERE", new YouTubePlayer.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.cueVideo(videoId); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }); } }
Breaking MainActvity
The following piece of code will set click listener on the play Button. When we will click on the play button it will take the id of video from EditText and pass it to methodplayVideo()Method.
buttonPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String videoId = editTextId.getText().toString(); playVideo(videoId, youtubePlayerView); } });
Inside playVideo()initialize YouTubePlayerView with API key. Then when onInitializationSuccess() callcueVideo() with id of video.
public void playVideo(final String videoId, YouTubePlayerView youTubePlayerView) { //initialize youtube player view youTubePlayerView.initialize("YOUR API KEY HERE", new YouTubePlayer.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.cueVideo(videoId); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }); }
Add internet permission inside yourmanifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Done now run the application.
If you still have any queries, please post them in the comments section below, I will be happy to help you.
Hello there, My name is Amardeep founder of loopwiki.com. I have experience in many technologies like Android, Java, Php, etc. In this variety of technologies, I love Android App Development. If you have any idea and you want me to develop for you then let's have chat Conatct
Can I Embed A Youtube Video In My App
Source: https://www.loopwiki.com/application/embed-youtube-video-in-android-app/
Posted by: davissawly1965.blogspot.com
0 Response to "Can I Embed A Youtube Video In My App"
Post a Comment