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

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

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时,一切正常。

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

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);
    }
}
英文:

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

答案1

得分: 11

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

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

<queries>
   <intent>
     <action android:name="com.google.android.youtube.api.service.START" />
   </intent>
</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:

&lt;queries&gt;
&lt;intent&gt;
&lt;action android:name=&quot;com.google.android.youtube.api.service.START&quot; /&gt;
&lt;/intent&gt;
&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:

确定