Updated 22 September 2023
If we want to show the video to our android application then we do not store the video inside the project, this will increase the app size. So instead, we can upload the video to YouTube and stream it in the app to decreases the app size.
So In this blog,
I have shown you how to play the video in your own android app.
We have been already interacting with Google APIs, So we need to get the Google Developer API Key first.
Follow below steps to obtain your Google Developer Android API Key.
Open the layout file of your activity (activity_main.xml) and add below code. This creates a simple layout with YouTubePlayerView.
For YouTubePlayerView
1 2 3 4 5 |
<com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtube_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="30dp" /> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true" tools:context=".MainActivity"> <TextView android:id="@+id/heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:gravity="center_horizontal" android:minWidth="150dp" android:paddingTop="50dp" android:text="My youtube player" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textColor="#808080" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" android:padding="20dp"> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtube_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="30dp" /> <Button android:id="@+id/start_video_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:minWidth="210dp" android:text="PLAY VIDEO STANDALONE" /> <CheckBox android:id="@+id/lightbox_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:minWidth="150dp" android:text="Lightbox mode" android:textColor="#808080" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout> |
A view for displaying YouTube videos. Using this View directly is an alternative to using theYouTubePlayerFragment
. If you choose to use this view directly, your activity needs to extend YouTubeBaseActivity
.
To get started, place this view in your view hierarchy and call initialize(String, OnInitializedListener)
to create a YouTubePlayer
which you can use to load videos into this View.
1 |
youTubeView.initialize(DEVELOPER_KEY, this); |
This view will save and restore the state of the YouTubePlayer
associated with the view as part of the onSaveInstanceState
/onRestoreInstanceState
flow.
This activity also contains few initialization listener methods to know the status of the youtube player.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
package testapplication.amangupta.youtubedemo; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CheckBox; 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 com.google.android.youtube.player.YouTubeStandalonePlayer; import java.util.List; public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, View.OnClickListener { private static final String DEVELOPER_KEY = "YOUR_DEVELOPER_KEY"; private static final String VIDEO_ID = "cdgQpa1pUUE"; private Button playVideoButton; private CheckBox lightboxModeCheckBox; private static final int REQ_START_STANDALONE_PLAYER = 1; private static final int REQ_RESOLVE_SERVICE_MISSING = 2; private YouTubePlayerView youTubeView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); playVideoButton = (Button) findViewById(R.id.start_video_button); lightboxModeCheckBox = (CheckBox) findViewById(R.id.lightbox_checkbox); youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view); // Initializing video player with developer key youTubeView.initialize(DEVELOPER_KEY, this); playVideoButton.setOnClickListener(this); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) { if (errorReason.isUserRecoverableError()) { errorReason.getErrorDialog(this, REQ_START_STANDALONE_PLAYER).show(); } else { String errorMessage = String.format( getString(R.string.error_player), errorReason.toString()); Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); } } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { // loadVideo() will auto play video // Use cueVideo() method, if you don't want to play it automatically player.cueVideo(VIDEO_ID); // Hiding player controls // player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS); } } @Override public void onClick(View v) { boolean lightboxMode = lightboxModeCheckBox.isChecked(); Intent intent = null; if (v == playVideoButton) { intent = YouTubeStandalonePlayer.createVideoIntent( this, DEVELOPER_KEY, VIDEO_ID, 0, false, lightboxMode); } 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(this, REQ_RESOLVE_SERVICE_MISSING).show(); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQ_START_STANDALONE_PLAYER) { // Retry initialization if user performed a recovery action getYouTubePlayerProvider().initialize(DEVELOPER_KEY, this); } if (requestCode == REQ_START_STANDALONE_PLAYER && resultCode != RESULT_OK) { YouTubeInitializationResult errorReason = YouTubeStandalonePlayer.getReturnedInitializationResult(data); if (errorReason.isUserRecoverableError()) { errorReason.getErrorDialog(this, 0).show(); } else { String errorMessage = String.format(getString(R.string.error_player), errorReason.toString()); Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); } } } private boolean canResolveIntent(Intent intent) { List<ResolveInfo> resolveInfo = getPackageManager().queryIntentActivities(intent, 0); return resolveInfo != null && !resolveInfo.isEmpty(); } private int parseInt(String text, int defaultValue) { if (!TextUtils.isEmpty(text)) { try { return Integer.parseInt(text); } catch (NumberFormatException e) { // fall through } } return defaultValue; } private YouTubePlayer.Provider getYouTubePlayerProvider() { return (YouTubePlayerView) findViewById(R.id.youtube_view); } } |
Maybe this tutorial is worked for you.
Source:
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.