Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube Player Api Reloads Video from start on switching to full screen

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:

  1. The video reloads from the beginning.
  2. On pressing the back button in full screen mode, the app is forced into landscape orientation.

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();
        }
    }
}
like image 408
A.B. Avatar asked Dec 08 '22 00:12

A.B.


1 Answers

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.

like image 118
Ludwig S Avatar answered May 17 '23 10:05

Ludwig S