我对YouTube有问题(总是加载(。
显然"但只是第一次"正常工作,我想说,第一个实例(第一次运行(在水平和垂直方向上都正常工作并正确加载,但当在我的MainActivity中更改fragment->fragmentManager.beginTransaction()
时(我在所有fragment上都有函数,将此方法调用为beginTransaction
(,并且在我转到->beginTransaction YouTube->后,视频总是加载:
示例:
步骤1
第一次->主活动->beginTransaction YouTube->点击->加载正确
步骤2
第二次->主活动->beginTransaction YouTube->点击->始终加载
关闭应用程序(菜单(,然后重复步骤1和2。
这是我的代码:
public void youtube(){
youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.cueVideo(VIDEO_ID);
youTubePlayers=youTubePlayer;
}
// Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
@Override
public void onFullscreen(boolean b) {
if(b){
//Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
MainActivity.isYoutubeReadyHoritzontal=true;
knowIfPortraitAndBackManyally=0;
}else {
//Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
knowIfPortraitAndBackManyally=1;
MainActivity.countIFexit=0;
//MainActivity.isYoutubeReadyHoritzontal=false;
}
}
});
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format("YouTube Error (%1$s)",
errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}
});
android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().replace(R.id.youtube_fragment,youTubePlayerFragment).commit();
MainActivity.isYoutubeReadyHoritzontal=true;
}
XML
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/youtube_fragment" >
我遇到了同样的问题,这是由于未能正确处理以前的播放器实例造成的。确保在不再需要youTubePlayer.release()
时调用它。