发生错误,当我将目标 SDK 升级到 30 时初始化 YouTube 播放器。

huangapple go评论106阅读模式
英文:

Error occurred while initializing the youtube player when i upgrade target sdk to 30

问题

我已经实现了YouTube API,一切运行正常。但是当我将

compileSdkVersion 29
升级到
compileSdkVersion 30

targetSdkVersion 29
升级到
targetSdkVersion 30

时,它给我一个错误:

在初始化YouTube播放器时发生错误。

我尝试从https://console.developers.google.com/更改我的API密钥,但这没有意义。当我将compileSdkVersion和targetSdkVersion再次设置为29时,一切正常。

这是我的视频播放器页面代码:

  1. package silver.yellow.yellowpamphletonline;
  2. import android.os.Bundle;
  3. import android.widget.Button;
  4. import com.google.android.youtube.player.YouTubeBaseActivity;
  5. import com.google.android.youtube.player.YouTubeInitializationResult;
  6. import com.google.android.youtube.player.YouTubePlayer;
  7. import com.google.android.youtube.player.YouTubePlayerView;
  8. public class VideoPlayer extends YouTubeBaseActivity {
  9. YouTubePlayerView videoPlayer;
  10. Button playButton;
  11. YouTubePlayer.OnInitializedListener onInitializedListener;
  12. boolean playing;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_video_player);
  17. videoPlayer = findViewById(R.id.videoPlayer);
  18. playButton = findViewById(R.id.play);
  19. playing = false;
  20. onInitializedListener = new YouTubePlayer.OnInitializedListener() {
  21. @Override
  22. public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
  23. playButton.setOnClickListener(v -> {
  24. if(playing){
  25. youTubePlayer.pause();
  26. playing = false;
  27. playButton.setText("Play");
  28. }
  29. else {
  30. youTubePlayer.play();
  31. playing = true;
  32. playButton.setText("Pause");
  33. }
  34. });
  35. youTubePlayer.loadVideo("pr4OEPhwM3c");
  36. playing = true;
  37. playButton.setText("Pause");
  38. youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
  39. }
  40. @Override
  41. public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
  42. }
  43. };
  44. videoPlayer.initialize(VideoPlayerConfig.API_KEY,onInitializedListener);
  45. }
  46. }
英文:

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:

  1. package silver.yellow.yellowpamphletonline;
  2. import android.os.Bundle;
  3. import android.widget.Button;
  4. import com.google.android.youtube.player.YouTubeBaseActivity;
  5. import com.google.android.youtube.player.YouTubeInitializationResult;
  6. import com.google.android.youtube.player.YouTubePlayer;
  7. import com.google.android.youtube.player.YouTubePlayerView;
  8. public class VideoPlayer extends YouTubeBaseActivity {
  9. YouTubePlayerView videoPlayer;
  10. Button playButton;
  11. YouTubePlayer.OnInitializedListener onInitializedListener;
  12. boolean playing;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_video_player);
  17. videoPlayer = findViewById(R.id.videoPlayer);
  18. playButton = findViewById(R.id.play);
  19. playing = false;
  20. onInitializedListener = new YouTubePlayer.OnInitializedListener() {
  21. @Override
  22. public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
  23. playButton.setOnClickListener(v->{
  24. if(playing){
  25. youTubePlayer.pause();
  26. playing = false;
  27. playButton.setText("Play");
  28. }
  29. else {
  30. youTubePlayer.play();
  31. playing = true;
  32. playButton.setText("Pause");
  33. }
  34. });
  35. youTubePlayer.loadVideo("pr4OEPhwM3c");
  36. playing = true;
  37. playButton.setText("Pause");
  38. youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
  39. }
  40. @Override
  41. public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
  42. }
  43. };
  44. videoPlayer.initialize(VideoPlayerConfig.API_KEY,onInitializedListener);
  45. }
  46. }

答案1

得分: 11

关于Android目标SDK版本30的问题是操作系统限制了对其他应用程序的访问。

在AndroidManifest的manifest标签中添加以下内容以解决YouTube问题:

  1. <queries>
  2. <intent>
  3. <action android:name="com.google.android.youtube.api.service.START" />
  4. </intent>
  5. </queries>

答案来源于:
https://stackoverflow.com/a/65070537/14747641

感谢 @Zahra Jamshidi

英文:

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:

  1. &lt;queries&gt;
  2. &lt;intent&gt;
  3. &lt;action android:name=&quot;com.google.android.youtube.api.service.START&quot; /&gt;
  4. &lt;/intent&gt;
  5. &lt;/queries&gt;

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

Thanks @Zahra Jamshidi

huangapple
  • 本文由 发表于 2020年8月9日 19:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63325493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定