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, 2022 in Android by Edureka
• 12,690 points
1,536 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, 2022 by Edureka
• 13,620 points

Related Questions In Android

0 votes
1 answer
–2 votes
0 answers

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

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

Nov 8, 2022 in Android by Edureka
• 13,620 points
1 flag 23,543 views
0 votes
1 answer

Why is applicationWillTerminate not being called on the first launch of my app?

The applicationWillTerminate method is called by the ...READ MORE

answered Mar 18, 2023 in Android by vishalini
1,358 views
0 votes
0 answers

Why does one use dependency injection?

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

Sep 20, 2022 in Android by Edureka
• 13,620 points
295 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, 2022 in Others by nisha
• 2,210 points
1,874 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, 2022 in Others by nisha
• 2,210 points
1,146 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, 2022 in Others by nisha
• 2,210 points
4,438 views
0 votes
1 answer
+1 vote
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, 2022 in Android by Edureka
• 13,620 points
8,088 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