4

I had implemented Youtube API and everything works great

but when I had upgraded

compileSdkVersion 29
to compileSdkVersion 30 and targetSdkVersion 29 to targetSdkVersion 30

it gives me An error occurred while initializing the youtube player

I tried to change my API key from https://console.developers.google.com/ but it doesn't make sense And everything goes well when I set compileSdkVersion and targetSdkVersion again to 29

Here is my Video Player Page Code:

package silver.yellow.yellowpamphletonline;
import android.os.Bundle;
import android.widget.Button;
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 VideoPlayer extends YouTubeBaseActivity {
    YouTubePlayerView videoPlayer;
    Button playButton;
    YouTubePlayer.OnInitializedListener onInitializedListener;
    boolean playing;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        videoPlayer = findViewById(R.id.videoPlayer);
        playButton = findViewById(R.id.play);
        playing = false;
        onInitializedListener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                playButton.setOnClickListener(v->{
                    if(playing){
                        youTubePlayer.pause();
                        playing = false;
                        playButton.setText("Play");
                    }
                    else {
                        youTubePlayer.play();
                        playing = true;
                        playButton.setText("Pause");
                    }

                });
                
                youTubePlayer.loadVideo("pr4OEPhwM3c");
                playing = true;
                playButton.setText("Pause");
                youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

            }
        };
        videoPlayer.initialize(VideoPlayerConfig.API_KEY,onInitializedListener);
    }
}
  • Add more details of the error you got. `An error occurred while initializing the youtube player` is not enough information. Check the answers on [this question](https://stackoverflow.com/q/15458351/4092887) and search `an error occurred while initializing the youtube player android` on Google. – Mauricio Arias Olave Aug 18 '20 at 15:49

1 Answers1

11

The issue with Android Target SDK Version 30 is the os is restricting access to other apps. https://developer.android.com/training/basics/intents/package-visibility

Add the following to manifest tag in AndroidManifest to fix YouTube issue:

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

And here is the answer from: https://stackoverflow.com/a/65070537/14747641

Thanks @Zahra Jamshidi

David
  • 126
  • 2
  • 4