Reputation:
I have a problem with YouTube (always loading).
Apparently works correctly "but only first time", I want say, the first instance (first run) works correctly and load correctly horitzontal and vertically, but when change fragment - > fragmentManager.beginTransaction()
in my MainActivity (I have function on all fragments calls this method to beginTransaction
) , and after I go to - > beginTransaction YouTube - > the video always load:
Example:
Step 1
First time - > MainActivity - > beginTransaction YouTube - > click - -> Load correctly
Step 2
Second time - > MainActivity - > beginTransaction YouTube - > click - -> Load always
Close App (Menu) and repeat Step 1 and 2 .
This is my code :
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" >
Upvotes: 6
Views: 822
Reputation: 4737
I had the same problem and it was caused by a failure to properly dispose of a previous player instance. Make sure to call youTubePlayer.release()
when it's no longer needed.
Upvotes: 1