question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save current Second & the Duration of the video when orientation change

See original GitHub issue

Hello,

I would like to ask you how can i setup the saved state of the current second and the duration of the video when an activity-fragment is re-created (orientation change). I use the onStateChange() to get the current second of the video and the video duration (and i’m storing them inside the onSaveInstanceState), but still when the orientation changes the video starts from 0 and after that is going to the saved current Video Second.

I tried to use youtubePlayer.seekTo(videoSecond) method to set the current second immediately, but without success…

Futhermore, i want to setup too the saved video Duration immediately when the orientation changes, but i can’t find the way…

Here is the method inside the Fragment:

private void initYouTubePlayerView() {        
    getLifecycle().addObserver(youtubePlayerView);

    youtubePlayerView.initialize(youTubePlayer -> {
        youTubePlayer.addListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady() {                   
                    loadVideo(youTubePlayer, mSuggestionYoutubeId);                    
            }
        });
        mTracker = new YouTubePlayerTracker();
        youTubePlayer.addListener(mTracker);
        youTubePlayer.addListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onStateChange(@NonNull PlayerConstants.PlayerState state) {
                super.onStateChange(state);
                videoSecondFloat = mTracker.getCurrentSecond();
                videoDuration = mTracker.getVideoDuration();                  
            }
        });
    }, true);

    fetchVideoTitle();
}

Here is the loadVideo Method:

 private void loadVideo(YouTubePlayer youTubePlayer, String videoId) {
        if(getLifecycle().getCurrentState() == Lifecycle.State.RESUMED)
            youTubePlayer.loadVideo(videoId, videoSecondFloat);
        else
            youTubePlayer.cueVideo(videoId, videoSecondFloat);
    }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
PierfrancescoSoffritticommented, Aug 2, 2018

Nice!

0reactions
KostasAnagnostoucommented, Aug 2, 2018

Yes… I did that and it’s working…

 youTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                @Override
                public void onStateChange(@NonNull PlayerConstants.PlayerState state) {
                    super.onStateChange(state);                  
                    if (state == PlayerConstants.PlayerState.PLAYING || state == PlayerConstants.PlayerState.PAUSED) {
                       // unhide the start time
                        videoIsReady();
                    }                    
                    videoSecondFloat = mTracker.getCurrentSecond();                    
                }
            });
        }, true);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Save Activity's State When Orientation Changes in ...
In this quick android tutorial, we use onSaveInstanceState() and onRestoreInstanceState() methods in order to temporary save information.
Read more >
When the orientation change from portrait to landscape, the ...
I am writing an Android project, at beginning I am playing a video in portrait mode, a few seconds later when i change...
Read more >
Change or lock the screen orientation on iPhone
You can lock the screen orientation so that it doesn't change when you rotate iPhone. Open Control Center, then tap the Lock Orientation...
Read more >
Handling Orientation Changes on Android | Medium
The most important aspect of handling orientation changes is saving state. In most cases this involves implementing the onSaveInstanceState ...
Read more >
Changing page orientation - Canva Help Center
Tap Resize to resize the current design. Depending on your design, you might be given the option to pick from two resized variations....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found