Why YouTube Player API does not work on Android 11

0 votes

When I tried to play YouTube videos on Android 9 and Android 10 devices, they played just fine. However, when I opened the YouTube video window on an Android 11 smartphone, I saw the following message:

"An error occurred while initializing the YouTube player".

This is the code of MainActivity.java:

package com.example.youtube;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
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 androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends YouTubeBaseActivity {

    Button play_btn;
    YouTubePlayerView youtubePlayerView;
    YouTubePlayer.OnInitializedListener onInitializedListener;

    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        youtubePlayerView = findViewById(R.id.youtubeView);
        play_btn = findViewById(R.id.playvideo_btn);

        final android.app.Activity myActivity = this;

        onInitializedListener =new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("Up9BjrIuoXY");
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                if (youTubeInitializationResult.isUserRecoverableError()) {
                    youTubeInitializationResult.getErrorDialog(myActivity, RECOVERY_DIALOG_REQUEST).show();
                } else {
                    String errorMessage = String.format(
                            getString(R.string.error_player), youTubeInitializationResult.toString());
                    Toast.makeText(myActivity, errorMessage, Toast.LENGTH_LONG).show();
                }
            }
        };

        play_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                youtubePlayerView.initialize("AIzaSyZ2", onInitializedListener);
            }
        });

    }
}

The YouTube app for Android 11 devices is updated.

Hope somebody could help me. Thanks!

Nov 14 in Android by Edureka
• 10,980 points
57 views

1 answer to this question.

0 votes
The Android 11 package visibility limitations prevent the SDK from finding the Youtube service unless you include a reference to it in.
answered Nov 15 by Edureka
• 12,390 points

Related Questions In Android

0 votes
0 answers

Android Webiew does not load indexed Sdcard (file://sdcard)

When attempting to use mobile browsers to ...READ MORE

Nov 8 in Android by Edureka
• 12,390 points
928 views
0 votes
0 answers

Why does one use dependency injection?

What are some actual circumstances when using ...READ MORE

Sep 20 in Android by Edureka
• 12,390 points
42 views
0 votes
0 answers

Invoke-customs are only supported starting with android 0 --min-api 26

Before, I was using Gradle build version ...READ MORE

Sep 21 in Android by Edureka
• 12,390 points
45 views
0 votes
1 answer

How to open YouTube video link in android app?

The below code will do that: Intent intent ...READ MORE

answered Jun 1 in Others by nisha
• 2,190 points
641 views
0 votes
1 answer
0 votes
1 answer

Android YouTube app Play Video Intent

The best way to run videos on ...READ MORE

answered Jun 6 in Others by nisha
• 2,190 points
244 views
0 votes
2 answers

Code for download video from Youtube on Java, Android

Check the source code (HTML) of YouTube, ...READ MORE

answered Jun 9 in Others by nisha
• 2,190 points
1,386 views
0 votes
1 answer
0 votes
1 answer

net: ERR_UNKNOWN_URL_SCHEME in Android Webview codes not working in Kotlin

3 ways to fix the err_unknown_url_scheme error. You ...READ MORE

answered Nov 8 in Android by Edureka
• 12,390 points
196 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP