Android YouTube API video pause when rotate screen or hide player
Asked Answered
H

6

8

I'm using the YouTube API for Android. The video is playing normally but when I turn the screen, the player stop the video and it comes back from the beginning as if it had not been played. The same happens when scroll the screen and hides the player. I've researched a lot about and also got to test some things I found but nothing worked =(

This is the way that i declared my class:

public class PlayerTest extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

This is what i put into onCreate():

 YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(DEVELOPER_KEY, this);

and this is the methods what i implement:

@Override
public void onInitializationFailure(Provider provider,
        YouTubeInitializationResult error) {
    Toast.makeText(this, "Error :( " + error.toString(), Toast.LENGTH_LONG)
            .show();
}

@Override
public void onInitializationSuccess(Provider provider,
        YouTubePlayer player, boolean wasRestored) {
    player.cueVideo(video);
}

question:

I want the video continue playing while scrolls or flips the screen. Is this possible? I'm implementing the right way? If not, what would be the best way?

Thank you in advance for help!

Hyetology answered 4/12, 2013 at 18:36 Comment(1)
As sqrfv points out, an Android Activity is destroyed and recreated when the device orientation changes. I suggest you use the getCurrentTimeMillis() method to find the current play position (perhaps in the Activity onPause() method) and save it, When the Activity is recreated, use the seekToMillis() method to restart playback at the last position.Sletten
R
2

When you flip the screen, the activity that was playing the video gets destroyed, and a new activity gets created with everything reset. Its a feature, not a bug. :)

There are a handful of ways of dealing with this; you will probably have to save off some state and then check for any saved data when any new activity gets created, manage the data, etc. Check out: Keeping data view/layout in Android after screen rotate/activity destroyed.

Rendezvous answered 4/12, 2013 at 18:39 Comment(7)
Thank you for the answer. As i said to @Sletten I had thought of doing something like this, but i didn't know about this feature. I'll try to do something like you said. This case of rotate the screen it's ok, i understand that is a feature. But in the case of scroll the screen and hide the player why video stop too? And why when the screen are in landscape the video play and pause immediately?Hyetology
For the scrolling issue we would need more info, actives are clearly not destroyed on a scroll event.Rendezvous
I noticed one thing: when I scroll the screen slowly and hide the player and then return to view the player, the player PAUSE and not STOP, just PAUSE. But if I scroll quickly and return the player keeps playing. What it suggests is that it seems that activity know if the component is being displayed or not, and if he is not showing it is destroyed. If scroll the screen to view component, the activity creates it again. But if so, the player was not for PAUSE but to STOP. Do you agree?Hyetology
I highly doubt the activity is destroyed on a scroll event if the view moves off the screen. You could put some logging into the activity's onCreate method to see exactly when it is being invoked. YoutubeAPI may be smart enough to pause or stop the view if the video is no longer on the screen, but if that is the case, the STOP command would reset the player to the start, while the PAUSE command would not.Rendezvous
Yes, I tested here. The component is not destroyed, is the youtube API that tells the player to PAUSE it when the player moves off the screen, the video is paused only, does not return to start. I did a test, I changed my xml layout to let the player fixed on the screen and scroll just to the rest of the content, now he no longer pause. It's an alternative =)Hyetology
Half of anything I program tend to be alternatives.Rendezvous
hehehe yes, i think this is the way... but thanks for help, your comments help me to find alternatives.Hyetology
E
18

What did the trick for me is to add:

android:configChanges="keyboardHidden|orientation|screenSize"

to your android manifest.

Cheers..

Epenthesis answered 25/9, 2015 at 21:4 Comment(2)
simple and fix the problemFornication
If i use your solution, then when i rotate the screen, it keep on using the portrait layout xml file instead of switching to the landscape layout fileCodeclination
S
5
<activity 
android:label="YoursActivityName" 
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.example.naren.YoursActivityName" /> 

Add it in yours manifest file.

Spermatium answered 25/2, 2015 at 9:14 Comment(0)
R
2

When you flip the screen, the activity that was playing the video gets destroyed, and a new activity gets created with everything reset. Its a feature, not a bug. :)

There are a handful of ways of dealing with this; you will probably have to save off some state and then check for any saved data when any new activity gets created, manage the data, etc. Check out: Keeping data view/layout in Android after screen rotate/activity destroyed.

Rendezvous answered 4/12, 2013 at 18:39 Comment(7)
Thank you for the answer. As i said to @Sletten I had thought of doing something like this, but i didn't know about this feature. I'll try to do something like you said. This case of rotate the screen it's ok, i understand that is a feature. But in the case of scroll the screen and hide the player why video stop too? And why when the screen are in landscape the video play and pause immediately?Hyetology
For the scrolling issue we would need more info, actives are clearly not destroyed on a scroll event.Rendezvous
I noticed one thing: when I scroll the screen slowly and hide the player and then return to view the player, the player PAUSE and not STOP, just PAUSE. But if I scroll quickly and return the player keeps playing. What it suggests is that it seems that activity know if the component is being displayed or not, and if he is not showing it is destroyed. If scroll the screen to view component, the activity creates it again. But if so, the player was not for PAUSE but to STOP. Do you agree?Hyetology
I highly doubt the activity is destroyed on a scroll event if the view moves off the screen. You could put some logging into the activity's onCreate method to see exactly when it is being invoked. YoutubeAPI may be smart enough to pause or stop the view if the video is no longer on the screen, but if that is the case, the STOP command would reset the player to the start, while the PAUSE command would not.Rendezvous
Yes, I tested here. The component is not destroyed, is the youtube API that tells the player to PAUSE it when the player moves off the screen, the video is paused only, does not return to start. I did a test, I changed my xml layout to let the player fixed on the screen and scroll just to the rest of the content, now he no longer pause. It's an alternative =)Hyetology
Half of anything I program tend to be alternatives.Rendezvous
hehehe yes, i think this is the way... but thanks for help, your comments help me to find alternatives.Hyetology
C
1

There is one approach that enables the video to play without any pauses during device rotation. Check out the YouTubeStandalonePlayer createVideoIntent() API with 'lightboxMode' parameter set to true.

If it is set to true, it plays the video in a dialog view above your current Activity and does not pause during device rotations.

Chamaeleon answered 18/7, 2014 at 7:37 Comment(0)
O
0

add player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT)


    override fun onInitializationSuccess(provider: YouTubePlayer.Provider, player: YouTubePlayer, wasRestored: Boolean) {
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT)
        if (!wasRestored) {
            player.loadVideo(videoId)
        }
    }

document https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer#FULLSCREEN_FLAG_CUSTOM_LAYOUT

avoiding the rebuffering that occurs in the default fullscreen behavior

Occam answered 2/10, 2019 at 11:45 Comment(0)
S
0
<activity android:name=".theActivityNameyouWantToApply" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"></activity>

just write this code in your Manifest part (remember to change the name) and it will work perfectly!!! thank you

Somaliland answered 24/6, 2021 at 19:9 Comment(1)
This is similar to the previous answer by Narendra. Please consider suggesting an edit for that answer with your reasons why your suggestion is better.Ironist

© 2022 - 2024 — McMap. All rights reserved.