I was trying to integrate the Youtube player api in my app. But when I switch to full screen there are a couple of problems that occur:
public class YouTubeVideoElement extends Element implements YouTubePlayer.OnInitializedListener {
private final String DEVELOPER_KEY = "MY_KEY";
private static final int RECOVERY_DIALOG_REQUEST = 1;
private RelativeLayout.LayoutParams layoutParams;
private YouTubePlayerFragment playerFragment;
private View playerView;
private ViewGroup viewFrame;
public State state;
private Dialog errorDialog;
private Context context;
public YouTubeVideoElement(Context context) {
this.context = context;
}
@Override
public String getType() {
return null;
}
@Override
public ViewGroup getView() {
return viewFrame;
}
@Override
public ViewGroup populateView(Context context, JsonObject data, Map<String, String> style) {
viewFrame = new FrameLayout(context);
playerView = new FrameLayout(context);
playerView.setId(R.id.player_view);
playerView.setVisibility(View.INVISIBLE);
viewFrame.addView(playerView, -1, -1);
playerView.setVisibility(View.VISIBLE);
playerFragment = YouTubePlayerFragment.newInstance();
playerFragment.initialize(DEVELOPER_KEY, this);
((Activity) context).getFragmentManager().beginTransaction().add(R.id.player_view, playerFragment).commit();
return viewFrame;
}
public RelativeLayout.LayoutParams getLayoutParams() {
return layoutParams;
}
public YouTubeVideoElement setLayoutParams(RelativeLayout.LayoutParams layoutParams) {
this.layoutParams = layoutParams;
return this;
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
Log.d("xxx", "onInitSuccess YTP");
if (!wasRestored) {
player.cueVideo("nCgQDjiotG0");
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
Log.d("xxx", "onInitFailure YTP");
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog((Activity) context, RECOVERY_DIALOG_REQUEST).show();
} else {
//String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
}
}
}
Please try and update this in your AndroidManifest file:
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.example.yourClassThatHandlesTheYoutubePlayer">
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
From the official android guidelines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With