英文:
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:
<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论