UnityPlugin-AVProVideo
UnityPlugin-AVProVideo copied to clipboard
I would like to be able to openMedia and have it seek to a specific time rather than starting at time 0
Is your feature request related to a problem? Please describe. I would like to be able to call OpenMedia and optionally include a time of when to seek to upon opeing/playing the media. In my code I had videoPlayer.Player.OpenMedia(mediaReference, autoplay); videoPlayer.Player.Control.Seek(startTime); This worked without issues in editor, but on an iPad4 it did not correctly seek to the time specified, so I had to create a work around to seek only after the media was fully loaded. But if I could OpenMedia(mediaReference, autoplay, seekTime) - or some variation of that, with consistant results across platforms, that would be fantastic.
Describe alternatives you've considered Currently I work around the issue by listenening for a MediaPlayerEvent.EventType.FirstFrameReady event, then calling my seek. But this workaround means the player is doing extra work I'm just going to throw away, and also requires I add additional variables to my code to track state (what my seek time should be, and whether I want to seek in a particular case or not).
Hey @bbangerter-s5d this seems to be the same functionality im trying to achieve, could you post a snipped of sample code?
Sure, see if this is helpful to you. Our system does do a fade to black before seeking, then fades back in - that is where the OnFadeIn function is coming from.
// These are needed to fix issues on ios playback that behave differently then playback in the editor
private float seekTime = -1;
private bool playOnSeek = false;
private void Awake()
{
videoPlayer.Player.Events.AddListener(OnHandleEvent);
}
private void OnOpenMedia(mediaReference, float startTime, bool autoplay)
{
// Check if we are playing a new video, or just seeking to a different time in the current one
if (videoPlayer.Player.MediaReference != mediaReference)
{
playOnSeek = false;
videoPlayer.Player.OpenMedia(mediaReference, autoplay);
seekTime = startTime;
}
else
{
seekTime = -1;
playOnSeek = autoplay;
OnFadeIn();
videoPlayer.Player.Control.Seek(startTime);
if (startTime > 0)
{
// On iPad4 - pause here and wait for a FinishedSeeking event to start playing again
videoPlayer.Player.Pause();
}
else if (IsPaused())
{
// on iPad4 - if we reach the end of the video and loop back to the beginning it does not trigger FinishedSeeking, so manually play here
videoPlayer.Player.Play();
}
}
}
private void OnFadeIn()
{
StartCoroutine(StartFadeIn());
// On iPad4 we can't call play followed immediately by seek, it doesn't seek. So instead we do our seek once we start fading back in
if (seekTime > 0)
{
videoPlayer.Player.Control.Seek(seekTime);
}
}
private void OnHandleEvent(MediaPlayer player, MediaPlayerEvent.EventType eventType, ErrorCode errorCode)
{
if (eventType == MediaPlayerEvent.EventType.FinishedSeeking)
{
if (playOnSeek) videoPlayer.Player.Play();
}
}
Thanks for this @bbangerter-s5d , where are you using OnOpenMedia/HandleMediaPlayerEvent here?
OnOpenMedia is driven from a mecanim state machine OnStateEnter. The state machine state knows which video to play and which time to seek too. The state machine itself is driven both by user input, and by time if the user just lets the video play without interacting with the UI.
For the HandleMediaPlayerEvent, do you mean the OnHandleEvent function provided above? Or something else?
Also, I realized I did leave out part of the important code in the OnHandleEvent. It should be
private void OnHandleEvent(MediaPlayer player, MediaPlayerEvent.EventType eventType, ErrorCode errorCode)
{
if (eventType == MediaPlayerEvent.EventType.FinishedSeeking)
{
if (playOnSeek) videoPlayer.Player.Play();
}
else if (eventType == MediaPlayerEvent.EventType.FirstFrameReady)
{
OnFadeIn();
}
}
(or maybe change the whole thing to a switch/case statement if there are other video player events you care about).
There is some other stuff we are doing, but it is related to enabling/disabling parts of the UI, and not relevant to the video player itself.
@bbangerter-s5d I mean HandleMediaPlayerEvent. I see in Awake that your script is only listening to OnHandleEvent, shouldnt it be both?
We are listening to all events sent from the AVPro video player. OnHandleEvent is the callback function when any event occurs, which then determines what to do based on the event type received if (eventType == MediaPlayerEvent.EventType.FinishedSeeking) ... else if (eventType == MediaPlayerEvent.EventType.FirstFrameReady) ...
Note that we did try limiting avpro to only notifiy us of those specific event types, but there is a bug in AVPro (which I logged a bug report for) where some of the events we were interested in did not trigger our event handler if we did not leave all events enabled. (I don't recall the exact details off the top of my head).
Gotcha thanks