我一直收到以下错误 - “网络出现了问题[400]”。
请参阅以下代码。
private void initializeYoutubeFragment() {
// in original code I am using my key here for sake of key safety I have replaced it
String apiKey = "MY_KEY";
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
YouTubePlayerSupportFragment youtubeFragment = YouTubePlayerSupportFragment.newInstance();
transaction.add(R.id.fl_video, youtubeFragment);
transaction.commitNow();
manager.executePendingTransactions();
youtubeFragment.initialize(apiKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cuePlaylist("5xVh-7ywKpE");
youTubePlayer.setPlayerStateChangeListener(callback);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
// no callback here
Log.e("TAG", "Fail");
}
});
}
更新1 我还在onInitializationSuccess方法中将YouTubePlayer.PlayerStateChangeListener添加到youtubeplayer实例。它调用onLoading方法然后继续调用onError(YouTubePlayer.ErrorReason)。 Heer ErrorReason.INTERNAL_ERROR即使在docs
中也没有任何解释回调代码
private YouTubePlayer.PlayerStateChangeListener callback = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {
Log.e("Tag", "loading");
}
@Override
public void onLoaded(String s) {
Log.e("Tag", "loaded");
}
@Override
public void onAdStarted() {
Log.e("Tag", "ad started");
}
@Override
public void onVideoStarted() {
Log.e("Tag", "Video Started");
}
@Override
public void onVideoEnded() {
Log.e("Tag", "video ended");
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
Log.e("Tag", "Error");
}
};