gpt4 book ai didi

安卓 YouTube API “An error occurred while initializing YouTube player”

转载 作者:行者123 更新时间:2023-11-29 01:32:29 31 4
gpt4 key购买 nike

我有一个包含 YouTubePlayerSupportFragment 的 fragment .此 fragment 位于 ViewPager 内。

播放器在我的 Galaxy S3 Android 手机 (4.3/API 18) 和我的虚拟机 (4.3/API 18) 上运行良好。但是在我尝试过的所有其他手机上,播放器都会显示 初始化 YouTube 播放器时发生错误 消息并抛出 onInitializationFailure有一个 UNKNOWN_ERROR 原因。

我已经检查了 LogCat,我的应用程序或 youtube 绝对没有其他错误/警告。

它不适用于以下设备:

  • Android 4.2.1 - Youtube 10.18.55
  • Android 4.4.2 - Youtube 10.18.55
  • Android 5.1 - Youtube 以上要求

我完全不知道为什么它对某些人有效而对其他人却无效。

fragment 代码如下:

    public class RoomFragmentVideo extends PlugFragment implements
YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener {
private static String CLASS_DBG_NAME = "RoomFragmentVideo";

private static final int RECOVERY_DIALOG_REQUEST = 1;

private YouTubePlayerSupportFragment youTubePlayer ;
private int playerInitialized;
private YouTubePlayer player;

static PlugFragment init(Bundle args) {
RoomFragmentVideo roomFrag = new RoomFragmentVideo();
// Supply val input as an argument.
roomFrag.setArguments(args);
Log.d(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - ","Created with args Room: " + ((String)args.get(RoomFragmentChat.ARG_ROOM)));
return roomFrag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thisActivity = (AppCompatActivity) this.getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_roomvideo, container,false);
youTubePlayer = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayer).commit();
youTubePlayer.initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
return rootview;

}

protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubePlayer;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
getYouTubePlayerProvider().initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
}
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this.getActivity(), 1).show();
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Recoverable] Reason : " + errorReason.toString());
}
else {
//String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
String errorMessage = "Video couldnt load";
Toast.makeText(this.getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Unrecoverable] Reason : " + errorReason.toString() + " Message: " + errorMessage);
playerInitialized = -1;
}

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
this.player = player;
player.setPlayerStyle(PlayerStyle.CHROMELESS);
player.loadVideo(this.videoID);
playerInitialized = 1;
}

@Override
public void onPause(){
super.onPause();
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onPause");
}

@Override
public void onResume(){
super.onResume();
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onResume");
}

@Override
public void onStop(){
super.onStop();
player = null;
playerInitialized = -2;
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onStop");
}

@Override
public void setUserVisibleHint (boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
this.resume();
}
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "setUserVisibleHint, status: " + isVisibleToUser);
}

@Override
public void onAdStarted() {
return;
}

@Override
public void onError(ErrorReason arg0) {
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " +"YoutubePlayer", arg0.name());
Toast.makeText(thisActivity, "YoutubePlayer error: " + arg0.name(), Toast.LENGTH_SHORT).show();
}

@Override
public void onLoaded(String arg0) {
return;
}

@Override
public void onLoading() {
return;
}

@Override
public void onVideoEnded() {
return;
}

@Override
public void onVideoStarted() {
return;

}
}

这是 XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:visibility="visible" />

</LinearLayout>

我做错了什么?

非常感谢!

最佳答案

问题实际上是proguard。这是我添加到 proguard-project.txt 文件中的内容:

# keep youtube / google
-keep class com.google.api.services.** { *; }
-keep class com.google.android.youtube.player.** { *; }
# Needed by google-api-client to keep generic types and @Key annotations accessed via reflection
-keepclassmembers class * {
@com.google.api.client.util.Key <fields>;
}

关于安卓 YouTube API “An error occurred while initializing YouTube player”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30428535/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com