Java – is there any way to restore Youtube API videos?

I'm using the Youtube API to write a simple application for Android – I want to realize the automatic recovery state of each video / live stream I will select in my application I can set automatic playback during initialization. The problem I encounter at present is that when I try to do like the following code, it doesn't do what I want (I play the button every time I use the application to enter the background and restore the display)

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnFullscreenListener,YouTubePlayer.OnInitializedListener {

    //private YouTubePlayer player = null;
    private boolean isFullscreen;
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private YouTubePlayerView youTubePlayerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Initializing YouTube Player View for displaying YouTube video **/
        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
        youTubePlayerView.initialize(DeveloperKey.DEVELOPER_KEY,this);

    }

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

        player.setFullscreen(true);
        player.setOnFullscreenListener(this);

        if (!wasRestored) {
            player.loadVideo(YouTubeVideoId.VIDEO_ID);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this,RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(getString(R.string.error_player),errorReason.toString());
            Toast.makeText(this,errorMessage,Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onFullscreen(boolean isFullscreen) {
        this.isFullscreen = isFullscreen;
    }

    @Override
    public void onResume() {
        super.onResume();
        youTubePlayerView.initialize(DeveloperKey.DEVELOPER_KEY,this);
    }

}

Thank you in advance for your kind help and advice!

Solution

I have solutions:

I have exactly the same problem. After jumping to the official example, I found that to my dismay, they have exactly the same problem External controls seem unable to interact with players in any important way Not to mention getting a fresh YouTube player is often problematic However, I try to find something by testing and trying different things:

Even if you save a static reference to YouTube player, this reference cannot change the player in any way But you can still retrieve where the video paused So, for me, the next logical step is to try this:

>Get a location from the player before leaving the event:

Integer myMILLIS = yplayer.getCurrentTimeMillis();

>Then, when you reload the video, move it to the correct position:

// Reload the SAME VIDEO again (yplayer is the YoutubePlayer)
yplayer.cueVideo(mVideoURL);
// Get the position you saved back
yplayer.seekToMillis(myMILLIS);
// Start Playing the video from that position again
yplayer.play();

The thing is, for some strange reason, it doesn't work alone, but when I try to debug previous code fragments... It ends up working sometimes

Therefore, after some testing, I found the problem of the previous solution: after the call

yplayer.cueVideo(mVideoURL);

Call anything else immediately after will fail So this may be a strange solution, but it does work perfectly:

>Get a location from the player before leaving the event:

Integer myMILLIS = yplayer.getCurrentTimeMillis();

>Then, when reloading the video:

// Reload the SAME VIDEO again (yplayer is the YoutubePlayer)
yplayer.cueVideo(mVideoURL);

>Before doing anything else, we have to wait:

// The next pause is here to allow the video to load
try
{
    Thread.sleep(1000);
} catch (InterruptedException e)
{
    // Do something here if you need to
}

>Move it to the correct position again and play:

// Get the position you saved back
yplayer.seekToMillis(myMILLIS);
// Start Playing the video from that position again
yplayer.play();

This solved my problem I hope it will solve your problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>