7
votes

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!

6
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.Squonk

6 Answers

2
votes

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.

17
votes

What did the trick for me is to add:

android:configChanges="keyboardHidden|orientation|screenSize"

to your android manifest.

Cheers..

5
votes
<activity 
android:label="YoursActivityName" 
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.example.naren.YoursActivityName" /> 

Add it in yours manifest file.

1
votes

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.

0
votes

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

0
votes
<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