Question

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!

Was it helpful?

Solution 3

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.

OTHER TIPS

What did the trick for me is to add:

android:configChanges="keyboardHidden|orientation|screenSize"

to your android manifest.

Cheers..

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

Add it in yours manifest file.

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.

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

<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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top