close fullscreen of Youtube Player on back button inside fragment
Asked Answered
D

2

7

I am using Master/Detail layout in a tablet, on the left few buttons to open several fragments, one of the fragments contains youtube player.

The Problem,

When the youtube player is full screen, and i press back button, The activity onBackPressed is called, and the whole activity is closed.

What i have tried,

1- Added on KeyListener for parent fragment (which contains the Youtube Fragment) and handle when click on back button, but this listener is called only if the player is not fullscreen, otherwise it is not called,

rootView.setFocusableInTouchMode(true);
        rootView.requestFocus();
        rootView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    try {
                        // Close full screen
                        return true;
                    } catch (Exception e) {
                        AHHExceptionHandler.handleException(e);
                    }
                }
                return false;
            }
        });

2- Added onKeyListener to youtubeFragment View to check if it is full screen then close the full screen mode

youTubeFragment.getView().setFocusableInTouchMode(true);
youTubeFragment.getView().requestFocus();
    youTubeFragment.getView().setOnKeyListener(new View.OnKeyListener() {
                                                @Override
                                                public boolean onKey(View v, int keyCode, KeyEvent event) {
                                                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                                                        try {
                                                            youTubePlayer.setFullscreen(false);
                                                            return true;
                                                        } catch (Exception e) {
                                                            AHHExceptionHandler.handleException(e);
                                                        }
                                                    }
                                                    return false;
                                                }
                                            });

And this is also not called in all cases.

I need to handle the hardware back button while the youtube player is in fullscreen, the fullscreen mode is closed and the application is in its previous state.

Thanks.

Edit 1: - I want to handle this inside the fragment, instead of handling it in the parent Activity, I already handling it inside the parent activity, but I don't like this solution.

Dearth answered 8/2, 2016 at 12:49 Comment(2)
Did you find a solution for this other than what you described here or adding youtube-specific code in the Activity onBackPressed?Millepede
No, I couldn't find another solutionDearth
K
4

You can override onBackPressed() in your activity and in case your player is in fullscreen, you can handle it there as follows:

@Override
public void onBackPressed() {

    if (youtubePlayer != null && isFullscreen) {
        // if fullscreen, set fullscreen false
        youtubePlayer.setFullscreen(false);

    } else {
        // if NOT fullscreen, perform default call on back press
        super.onBackPressed();
    }
}

P.S. In above code fragment, isFullscreen is instance variable of our activity and is synced with youtubePlayer state from within OnFullscreenListener set on youtubePlayer.setOnFullScreenListener().

Hope that makes sense :)

Katekatee answered 8/2, 2016 at 13:5 Comment(2)
The activity has no relation with the youtube player, the youtube player is inside fragment, And i don't want to handle this in the activity. I need to make code independent as i can.Dearth
The youtube player is in relation with the activitys context. Wish the best of luck to find what you are searching for.Katekatee
L
5

Just to add to Andre's reply.

In your parent Activity add:

public YouTubePlayer youTubePlayer;
public boolean isYouTubePlayerFullScreen;

In your fragment add:

MainActivity mainActivity = (MainActivity) getActivity();

Then, on YouTubePlayerFragment initialization:

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
        mainActivity.youTubePlayer = player;
        player.loadVideo(videoId);

        player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
            @Override
            public void onFullscreen(boolean b) {
                mainActivity.isYouTubePlayerFullScreen = b;
            }
        });

    }

Now your Activity has access to YouTubePlayer and information if player is full screen or not.

Next, inside your Activity, override onBackPressed like this:

@Override
public void onBackPressed() {
    if (youTubePlayer != null && isYouTubePlayerFullScreen){
            youTubePlayer.setFullscreen(false);
    } else {
        super.onBackPressed();
    }
}
Ladew answered 27/10, 2017 at 14:36 Comment(3)
this answer missing the static modifier for the public variables youTubePlayer & isYouTubePlayerFullScreenMartine
@EmadMorrisZedan, please elaborate why they have to be static.Ladew
Awsome answer, thanks Also you need to put in manifest file <activity android:name=".MainActivity" android:configChanges="orientation|screenSize" android:screenOrientation="portrait">Murderous
K
4

You can override onBackPressed() in your activity and in case your player is in fullscreen, you can handle it there as follows:

@Override
public void onBackPressed() {

    if (youtubePlayer != null && isFullscreen) {
        // if fullscreen, set fullscreen false
        youtubePlayer.setFullscreen(false);

    } else {
        // if NOT fullscreen, perform default call on back press
        super.onBackPressed();
    }
}

P.S. In above code fragment, isFullscreen is instance variable of our activity and is synced with youtubePlayer state from within OnFullscreenListener set on youtubePlayer.setOnFullScreenListener().

Hope that makes sense :)

Katekatee answered 8/2, 2016 at 13:5 Comment(2)
The activity has no relation with the youtube player, the youtube player is inside fragment, And i don't want to handle this in the activity. I need to make code independent as i can.Dearth
The youtube player is in relation with the activitys context. Wish the best of luck to find what you are searching for.Katekatee

© 2022 - 2024 — McMap. All rights reserved.