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 a crash issue on Android "Fatal Exception: java.lang.IllegalStateException" #353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class VideoFragment extends YouTubePlayerFragment {
public VideoFragment() {}


public void setYoutubeView(YouTubeView youTubeView) {
public void setYouTubeView(YouTubeView youTubeView) {
mYouTubeView = youTubeView;
}

public static VideoFragment newInstance(YouTubeView youTubeView) {
VideoFragment fragment = new VideoFragment();
fragment.setYoutubeView(youTubeView);
fragment.setYouTubeView(youTubeView);
return fragment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ public void onVideoFragmentResume() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
mYouTubePlayer.play();
try {
mYouTubePlayer.play();
} catch (IllegalStateException e) {
mYouTubeView.initPlayer();
}
}
}, 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class YouTubeView extends FrameLayout {
private YouTubePlayerController mYouTubeController;
private VideoFragment mVideoFragment;
private boolean mHasSavedInstance = false;
private String mApiKey;

public YouTubeView(ReactContext context) {
super(context);
Expand All @@ -32,6 +33,7 @@ public void init() {
inflate(getContext(), R.layout.youtube_layout, this);
mVideoFragment = VideoFragment.newInstance(this);
mYouTubeController = new YouTubePlayerController(this);
mApiKey = null;
}

@Nullable
Expand All @@ -45,7 +47,7 @@ protected Parcelable onSaveInstanceState() {
protected void onAttachedToWindow() {
if (!mHasSavedInstance) {
FragmentManager fragmentManager = getReactContext().getCurrentActivity().getFragmentManager();
fragmentManager.beginTransaction().add(getId(), mVideoFragment).commit();
fragmentManager.beginTransaction().add(getId(), mVideoFragment).commitAllowingStateLoss();
}
super.onAttachedToWindow();
}
Expand Down Expand Up @@ -153,14 +155,19 @@ public void didChangeToFullscreen(boolean isFullscreen) {
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "fullscreen", event);
}

public void setApiKey(String apiKey) {
public void initPlayer() {
try {
mVideoFragment.initialize(apiKey, mYouTubeController);
mVideoFragment.initialize(mApiKey, mYouTubeController);
} catch (Exception e) {
receivedError(e.getMessage());
}
}

public void setApiKey(String apiKey) {
mApiKey = apiKey;
initPlayer();
}

public void setVideoId(String str) {
mYouTubeController.setVideoId(str);
}
Expand Down