Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/android youtube player released #336

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package com.inprogress.reactnativeyoutube;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.view.View;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayer;


public class VideoFragment extends YouTubePlayerFragment {
public class VideoFragment extends YouTubePlayerFragment implements YouTubePlayer.OnInitializedListener {

private YouTubeView mYouTubeView;
private boolean mPlayerReleased = true;

private String mApiKey = null;
private YouTubePlayer.OnInitializedListener mInitializationListener = null;

public VideoFragment() {}

Expand All @@ -28,4 +39,76 @@ public void onResume() {

super.onResume();
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
/*
* According to the Youtube Api documentation :
* The YouTubePlayer associated with this fragment will be released
* whenever its onDestroyView() method is called.
*
* If we do not initialize again the player, it will crash telling that
* the youtube player has been released
*/
if (mPlayerReleased) {
initialize(mApiKey, mInitializationListener);
/* Now that the player has been initialize we track that we can use it */
mPlayerReleased = false;
}
return v;
}

@Override
public void onDestroyView() {
/*
* According to youtube Player api :
* The YouTubePlayer associated with this fragment will be released
* whenever its onDestroyView() method is called.
*
* This is important since it cause Exception : The youtube player has been release
* on some cases
*
* We keep track of this release in order to do a proper initialization
* back again at view creation
*/
mPlayerReleased = true;
/*
* Tell out view that the player is release in order to stop all actions
* on the youtube player until next initialization
*/
mYouTubeView.onPlayerRelease();
super.onDestroyView();
}

@Override
public void initialize(String developerKey, YouTubePlayer.OnInitializedListener listener) {
/*
* Inform the player that an initialization has started
*/
mYouTubeView.onInitializationStarted();
/*
* Save the developer key and the listener for later use
* (in onCreateView after a onDestroyView occurs)
*/
mApiKey = developerKey;
mInitializationListener = listener;

/* Now we call the actual API */
super.initialize(developerKey, this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (mInitializationListener != null) mInitializationListener.onInitializationSuccess(provider, youTubePlayer, wasRestored);
/* Keep track that the player is now available */
mPlayerReleased = false;
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
if (mInitializationListener != null) mInitializationListener.onInitializationFailure(provider, result);
/* Keep track that the player is not available */
mPlayerReleased = true;
}
}
Loading