Android YouTube API video pause when rotate screen or hide player

bertelli.deris picture bertelli.deris · Dec 4, 2013 · Viewed 13.1k times · Source

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!

Answer

Sindri Þór picture Sindri Þór · Sep 25, 2015

What did the trick for me is to add:

android:configChanges="keyboardHidden|orientation|screenSize"

to your android manifest.

Cheers..