RenderHeads / UnityPlugin-AVProVideo

AVPro Video is a multi-platform Unity plugin for advanced video playback
https://www.renderheads.com/products/avpro-video/
235 stars 28 forks source link

Mac: How to open then immediately start playing a video at a given time? #636

Closed TheloTheGreat closed 3 years ago

TheloTheGreat commented 3 years ago

On Mac, I can't figure out how to reliably open then start playing a video at a time other than T:0. For instance, if I want to start playing a video immediately on T:3s, I can do the following on Windows:

_mediaPlayer.OpenMedia(mediaRef, autoPlay: false); _mediaPlayer.Control.Seek(3.0); _mediaPlayer.Play();

But if I try to do this on Mac, the video always starts from the beginning, rather than from 3s. I need to wait some unknown amount of time after Play before doing the seek so that it works.

If I do the seek-then-play in reaction to a Started event, the seek fails. But if I do the seek-then-play event in reaction to a FirstFrameReady event, the seek-then-play does succeed.

This is all confusingly complex enough that I suspect I am just not understanding something fundamental in the whole process. So I ask the question: how can I reliably open a video, then immediately (as fast as possible) start playing that video at some given time?

Edit: This is AVPro 2.0.7 on Mac Unity (2020.2.1)

MorrisRH commented 3 years ago

Currently the best way would be to issue the Seek and Play calls from the ReadyToPlay event. FirstFrameReady is a bit late as the player would already have grabbed the frame at time 0 so you're likely to see a flicker when it updates.

We are currently looking into ways to make this more consistent across platforms.

TheloTheGreat commented 3 years ago

Thanks, that does seem to work. I do see a flash of another frame when I do that, but I'm not sure if it's the first frame of the video or the previous keyframe to the seeked time. It should be good enough.

MorrisRH commented 3 years ago

Support for calling Open, Seek, Play on macOS has now been added and will be available in the next release.

TheloTheGreat commented 3 years ago

Testing this on the 2.0.8 trial package, this still doesn't work quite right. If I add the following to MediaPlayerUI.Update:

            if (Input.GetKeyDown(KeyCode.W)) {
                _mediaPlayer.CloseMedia(); // does not matter
                _mediaPlayer.OpenMedia(MediaPathType.RelativeToProjectFolder, "Assets/StreamingAssets/AVProVideoSamples/BigBuckBunny-360p30-H264.mp4", autoPlay: false);
                _mediaPlayer.Control.Seek(3.0);
                _mediaPlayer.Control.Play();
            }

Then if I press W once, that works fine. But if I press W twice in quick succession, the second press will often fail to seek correctly, instead it will play from the beginning.

TheloTheGreat commented 3 years ago

Note: when that happens, there is a StartedSeeking event that gets triggered, but no corresponding FinishedSeeking event.

TheloTheGreat commented 3 years ago

New note: when that bad seek happens, there is a StartedSeeking event, then a ResolutionChanged event. When the seek works correctly, there is a ResolutionChanged event before the StartedSeeking event.

It kind of looks like whatever is doing ResolutionChanged interrupts the seek.

TheloTheGreat commented 3 years ago

Third note: This can actually also happen when I open the video normally, then call Play, then when I get a FirstFrameReady event I call a Seek. There's a StartedSeeking event, then a ResolutionChanged event (?!?), and my seek never completes.

MorrisRH commented 3 years ago

This should be fixed in the next release.

TheloTheGreat commented 3 years ago

Fix basically confirmed in 2.0.9, thanks. That said, 2.0.9 adds this new issue that seems related: Open-then-play no longer plays if there is no Seek operation called before the play: https://github.com/RenderHeads/UnityPlugin-AVProVideo/issues/664