PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.4k stars 758 forks source link

onBackPressed not working for some reason #689

Open TavernKeeper20 opened 3 years ago

TavernKeeper20 commented 3 years ago

Here's my code:

I bring in the reference from XML:

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);

Then the "If" statements:

if (webView.canGoBack()) {
            webView.goBack();
        } else if (youTubePlayerView.isFullScreen()) {
            youTubePlayerView.exitFullScreen();
        } else{
            super.onBackPressed();
        }

Here's my YT player in XML:

<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:translationZ="1dp"
        app:enableLiveVideoUi="true"
        app:autoPlay="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:videoId="5qap5aO4i9A">

    </com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView>
PierfrancescoSoffritti commented 3 years ago

From your message it's not clear what is not working.

TavernKeeper20 commented 3 years ago

I'll try to explain it better this time

So, when the Youtube player is in fullscreen and you click on the back button on your phone, it closes the app. I had similar issue with Webview - when site was loaded and you clicked on an article and then wanted to go back and pressed the back button, the app closed down (not shutdown, but you went to homescreen).

So I added an if statement to the onBackPressed function in my MainActivity.Java like so:

WebView webView = findViewById(R.id.webView); if (webView.canGoBack()) { webView.goBack(); } else{ super.onBackPressed(); }

Now, you can navigate in webView

I want to do the same with the Youtube player. Basically, whenever the player is in fullscreen and the user presses the "back button", currently the app closes down, but I want it to first check if the player is in fullscreen and if it is, have it exit the fullscreen.

To achieve that, I added similar code like so:

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view); if (youTubePlayerView.isFullScreen()) { youTubePlayerView.exitFullScreen(); } else { super.onBackPressed(); }

But it seems to not work and I'm not sure why? My project is in Java and the library is in Kotlin. Can that be what's causing this? I'm not getting any error.

PierfrancescoSoffritti commented 3 years ago

The problem is not with kotlin.

If you enter the else statement, it seems that the player is not actually fullscreen. You can put a breakpoint inside YouTubePlayerView to double check what's happening.

TavernKeeper20 commented 3 years ago

How do I listen to fullscreen button?