Why YouTube Player API does not work on Android 11?

末鹿安然 提交于 2021-01-27 07:00:12

问题


I tried the project explained on this YouTube video of how to play YouTube videos on Android apps:

https://www.youtube.com/watch?v=Up9BjrIuoXY

I tried to play YouTube videos with devices using Android 9 and Android 10 and the videos are played correctly, but in Android 11 devices I received this message inside the YouTube video window:

"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);
            }
        });

    }
}

YouTube app of the Android 11 devices is updated.

Hope somebody could help me. Thanks!


回答1:


With the package visibility restrictions in Android 11 the SDK wont be able to find the Youtube service unless you add a reference to it to your manifest.

<queries>
   <intent>
     <action android:name="com.google.android.youtube.api.service.START" />
   </intent>
</queries>



回答2:


Well. At last we have decided to use UniWebView Unity package and YouTube JavaScript API. Our project is targeted for iOS and Android.



来源:https://stackoverflow.com/questions/63909770/why-youtube-player-api-does-not-work-on-android-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!