Search Unity

Video The recommended way to seek time

Discussion in 'Audio & Video' started by roumenf, May 18, 2017.

  1. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    Hi guys,

    I'll try to summarize several questions regarding the time-seek in one post, in order not to spam the forum:

    1. What would be the recommended way to set time with 'VideoPlayer.time'-property? I mean, if the video is playing, should it be paused before setting time and then resumed, or not? Should VideoPlayer.Prepare() be called after setting the new time? If yes, should the preparation be awaited before resuming the video or setting a new time? Should Prepare() be used at all, and what happens if the time is changed in the meanwhile?

    2. Because our video-playing project must work on several platforms, I regularly test the VideoPlayer-component updates (my latest checks were with Unity v5.6.1 though). Setting video time on iOS and MacOSX has no tangible issues, but changing time on Android and Windows (incl. UWP) almost always caused video and audio to unsync. Is this an issue of the VideoPlayer, or is it related to some specifics of the AudioSource-component? What would be the way to work around this issue?

    3. I noticed that sometimes on Windows and Android, when I change the time, both video and audio may remain blocked. The VideoPlayer.time (and frame numer) continues to change, but the frame displayed remains the same and the audio is silent. The video looks like frozen. Any ideas how to work around this case?

    By the way, thank you for the great component! Just don't stop its development and bug-fixing.
     
  2. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Hiya.

    We just created a tutorial that covers this. The first real tutorial that covers the main properties, methods, and events in the new video player - and how to use them in a real example.

    Provide feedback, share, and let us know if we missed anything critical.

     
    oakesm and roumenf like this.
  3. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    roumenf likes this.
  4. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    I haven't done much unit testing with the audio, but I do know that seeking when playback speed > 1 can cause some unwanted results. Are you doing anything with playback speed with your audio goes out of sync?
     
  5. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    Thanks for the fast replay!

    My playback speed is set to 1 and won't be set anywhere in my code.
     
  6. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    @renaissanceCoder1 Thank you for the tutorial! I find it very useful for VP beginners, and it also gives me many ideas what to improve and what to try next. And of course, I liked it right away ;) As to this tutorial Prepare() should be called only once, when the video is loaded, correct? I currently call it after the time seeks as well, because otherwise, if the video is paused, the picture on screen doesn't change when I do time seek. May that be wrong?

    @Knusper Sorry, I missed the other thread regarding the audio going out of sync. This is what I'm experiencing too, especially when time seeks come into play.

    I don't think the out-of-sync issue is related to the playback speed. It is platform specific as well. On MacOS X and iOS there are no out-of-sync issues, according to my tests. But on Android, Windows & UWP - the issue is there. This issue could be seen best, if you play a video with a "talking head", like for instance a big part of this video: http://ratemt.com/downloads/sophie-haber2.mp4 It is in German, but this doesn't matter. After 1-2 seeks you can see how the lady's lips get out of sync with what she says.

    I also think @DominiqueLrx has already shed some light on audio processing in the other thread, and they are aware of the issue. Maybe it is also due to the fact that audio is processed in the main thread. I hope he could say soon that they have finally fixed the audio issues on these important platforms. I definitely don't want to cause them more stress than needed. In comparison with the initial releases, the VP-component progress is more than visible...
     
  7. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    I have written a bug report to unity because I don't know if they are aware of this problem.
    I'm just wondering that this issue is still present in the current beta of 2017 (video was introduced in 5.6) because it's not mentioned as experimental release. Something like seeking to a specific time is an essential part of video players.

    If no unity representative will answer here, I'll update the status of the bug report as soon as possible.
    But a workaround or a estimate when the problem will be solved would be great for me and my contract partners.
     
  8. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    Status:
    The bugreport has gotten closed because it has already been sent before.
    If you haven't voted for the issue i'd suggest to do that to increase the importance of the bug.

    The bug report can be found here:
    https://issuetracker.unity3d.com/issues/setting-videoplayer-dot-frame-from-code-offsets-audio

    Sadly there is no timeframe when the bug will be fixed or if a workaround will be made in the near future. Also it doesn't exist in the current 2017 beta changelog.
    Hoping for more information soon.

    Best,

    Knusper
     
    roumenf likes this.
  9. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    Right away. It is active, as far as I see.
     
    Knusper likes this.
  10. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    290
    Video team is aware of this issue and are working on it.
    Android and Windows are mostly affected while OSX and iOS don’t have this effect when using the “direct” audio output mode for all supported formats except webm/vp8 (which use the same desync-causing implementation as Android/Windows).

    Unfortunately the fix will not be in 2017.1, but we will try to fix this asap.
     
  11. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    That's great news.
    What are the chances to get the fixes in a patch release or in 2017.2?
     
  12. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    Sorry for the double post.
    But as this is an essential feature for a useful video system (which is integrated since months) I need to know if there is any workaround for this issue. The description in bugtracker has a severity of 3 which suggests that a workaround is possible. But I couldn't figure out how. Is this just a misleading message or is there a workaround that hasn't been communicated before?

    Best,

    Knusper
     
  13. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    According to the info I got from @valyard and @DominiqueLrx at Unite, a workaround for the issue may go like this:
    "Call Stop(), then Prepare(), wait for prepareCompleted event, then seek, and then Play() may work. The idea is to drain the accumulated audio sample queue before seeking. This is what causes the issue."

    So far, my implementation of this workaround was not very successful, but I need to check it once again.

    The fix (according to the same info) was planned for Unity 2017.3, i.e. at the end of 2017 or beginning of 2018. I think it is just too late, for fixing such a major video-player issue.
     
  14. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    I have also tried this workaround before. But even after following the steps exacly as you described I had no success. 2017.3 is a bit late for a reproduceable and serious bug for such an essential feature. Even because unity is advertizing with that feature :(

    We will have to wait now. If you have any sucesses with your workaround, please let me know! :)

    Best,

    Knusper
     
  15. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    @Knusper, FYI: Here is the seek-coroutine I added, according to the workaround instructions above:

    Code (CSharp):
    1.     private IEnumerator SeekTimeRoutine()
    2.     {
    3.         videoSeekActive = true;
    4.         videoSeekWasPlaying = isPlaying;
    5.  
    6.         // do as instructed in the mail from Valentin Simonov & Dominique Leroux (28.06.2017)
    7.         // DominiqueLRX: the idea is to drain the accumulated audio sample queue before seeking
    8.         if (videoPlayer)
    9.         {
    10.             // stop the player
    11.             videoPlayer.Stop();
    12.             if (audioSource)
    13.                 audioSource.Stop();
    14.             //isPlaying = false;
    15.             yield return null;
    16.  
    17.             // prepare the video player
    18.             videoPlayer.Prepare();
    19.  
    20.             // wait until video is prepared
    21.             Debug.Log("Preparing the video...");
    22.  
    23.             float waitTillTime = Time.time + 5f;  // wait 5 seconds max
    24.             while (!videoPlayer.isPrepared && (Time.time < waitTillTime))
    25.             {
    26.                 yield return null;
    27.             }
    28.  
    29.             string sMessage = videoPlayer.isPrepared ? "Video prepared" : "Video NOT prepared";
    30.             Debug.Log(sMessage);
    31.  
    32.             // start playing (otherwise it starts from time/frame 0).
    33.             videoPlayer.Play();
    34.             videoPlayer.Pause();
    35.  
    36.             yield return null;
    37.  
    38.             videoPlayer.time = videoSeekTime;
    39.             Debug.Log(string.Format("VideoPlayer time set to: {0:F3}", videoSeekTime));
    40.             videoSeekTime = -1.0;
    41.             //yield return null;
    42.         }
    43.  
    44.         videoSeekActive = false;
    45.     }
    46.  
    And then in the seek-completed handler, start playing the video:

    Code (CSharp):
    1.     void VideoSeekCompleted(VideoPlayer source)
    2.     {
    3.         Debug.Log("Seek completed. Time=" + source.time + ", frame: " + source.frame);
    4.  
    5.         if(videoSeekWasPlaying)
    6.         {
    7.             videoPlayer.Play();
    8.             if (audioSource)
    9.                 audioSource.Play();
    10.             isPlaying = true;
    11.         }
    12.     }
    13.  
    Unfortunately the success is only partial. Sometimes after seek the video and audio start synchronous, but in most cases - not. The video seek time and frame match. I don't know how to get audio frame info, so I could compare.

    @DominiqueLrx, could you please comment, and if possible check the code above for errors or provide your own workaround code and more tips. This is a MAJOR bug in the video player, which can ruin our project. I can provide an English-speaking video, if needed.
     
  16. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    I've recently tested Beta 2017.3b03.

    While the bug tracker still mentions the issue as active, I can say that seeking a video now works as expected without any desynchronization bugs!
    Great Job, Unity devs! Now the video player is a full usable tool for me :)

    I left this post just as a small hint to everyone who's running into that issue. I think the thread can be closed once v2017.3 is out in a final stable release.
     
    roumenf and ovirta like this.
  17. KMettes

    KMettes

    Joined:
    Jul 19, 2017
    Posts:
    1
    That it is fixed in the 2017.3b3 is great but we got other weird UI bugs like the RectMask2D that stopped working and the Aspect Ratio fitter that suddenly shoves other unrelated UI content downwards every frame...

    We still got to make a bug report for that, but for every bug report, we have to send a slimmed down project which just costs us a lot of time to slim down. We actually wanted to release months ago but with the constant ongoing bugs with Google VR, Video Player and UnityWebRequest on both iOS and Android our project has been delayed for months already waiting for Unity to fix their bugs.
    An example is the past few patches and releases contained a UI bug where a button action would be executed multiple times when it contained more than 1 action. like if it contained 3 actions, each action is executed 3 times.
    This has been fixed in 2017.3, great but we cannot use an engine that is only in beta. Who knows what other bugs will arise in the meanwhile.
    We already made a lot of workarounds in 2017.1 to fix some of the issues. We like to keep the engine up to date but due to the constant bugs the product is in an unreleasable state.
    We love using Unity but it would be great if they test the functionality and fix the bugs before actually releasing the features.

    Unity could learn from the EasyMovieTexture, the problem with that asset is that it doesn't have streaming and the code base behind it is a huge mess. Atleast the EMT asset has fluent seeking and does not bug out as much.
     
    Last edited: Oct 10, 2017
    roumenf likes this.
  18. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    You are particulary right.
    I think praising a feature like a whole video system with obvious bugs like not being able to seek without audio delay is not professional. That's something they definitely MUST have known by releasing the feature as final (at least if they have a minimum of QA). Resolving that issue a whole year after it started is a bad joke.

    But even if Unity has a lot of bugs it's definitely going better than it was. In times of Unity 4 there were so much bugs that developing complex games were a nightmare. Currently Unity is doing a good job in moving forward to a high quality engine by supporting a lot more platforms than any other engine. It offers a much broader range of features than other engines (XR, mobile performance, cloud networking, cloud builds, ads, just to name a few).

    So I think what Unity needs is to still improve it's Quality Assurance. It became a lot better but it seems that features still need more testing (or features must be held back until they are REALLY final)
     
    roumenf likes this.
  19. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    Is your video source a VideoClip or URL? I use URL and the (new) issue I'm facing on 2017.3b3 & b4 is that the audio is not playing at all. If the source is VideoClip, there is no issue. Any experience or suggestions?
     
  20. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    My source is a URL as we are streaming our video via StreamingAssets.
    Have you tried using an AudioSource instead Direct Audio Output?
     
  21. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    I have always used AudioSource. Actually, I found a workaround for this.
    But now I have the feeling, while the issue on Windows got fixed, a similar unsync issue was introduced on MacOS X.
     
  22. Tryz

    Tryz

    Joined:
    Apr 22, 2013
    Posts:
    3,225
    I'm running into this in Android too. I just tried Unity 2017.3.0b10 and still no luck. Setting VideoPlayer.time = x just "freezes" the video.

    I tried some of the suggestions on the forum with no luck. Has anyone found a solution other than waiting?

    Thanks
     
  23. roumenf

    roumenf

    Joined:
    Dec 12, 2012
    Posts:
    564
    FYI: Here is my workaround at the moment:

    - on Windows I use the fixed VideoPlayer from Unity 2017.3.0b6. I suppose it still works in the latest beta.
    - on Mac-OS X and iOS - the VideoPlayer from Unity 2017.2.0f3, because it worked perfectly back then.
    - on Android - the UMP-package for Android I found on Asset store: https://www.assetstore.unity3d.com/en/#!/content/56044 Yes, it costs some money, but it works and is easy to integrate. It is also for iOS, but I have not tried it there yet.

    Of course, you can wait they fix VideoPlayer on all platforms, but given the speed of fixing bugs so far, you'd better be prepared to wait. :)
     
    Tryz likes this.
  24. Tryz

    Tryz

    Joined:
    Apr 22, 2013
    Posts:
    3,225
    Thanks @roumenf . I'll give that a try.
     
  25. Knusper

    Knusper

    Joined:
    Jun 5, 2014
    Posts:
    35
    I'm currently using b09. This version also works fine for me. I haven't tried out Version b10.
     
  26. Tryz

    Tryz

    Joined:
    Apr 22, 2013
    Posts:
    3,225
    For Android, @roumenf said it's not working for him and he's using the UMP package as an alternative. Are you saying the native Unity VideoPlayer on Android is seeking fine for you?

    I noticed on small videos, it seeks slow... but eventually plays. Larger videos (1920x1080 3+ min) are freezing on Android like the original post's point #3.
     
  27. ben06feb

    ben06feb

    Joined:
    Oct 2, 2012
    Posts:
    28
    Still its not working fine with latest Unity. Is there any workaround for that. Seeking is lagging, sometimes stuck.
     
  28. Spanky11

    Spanky11

    Joined:
    Mar 9, 2015
    Posts:
    307
    I'm Streaming a YouTube video in 2017.3.1p1, and am trying to set the frame using:
    videoPlayer.frame=1234;

    When I do this, the audio keeps playing uninterrupted from whereever it was before I set the frame.
    The thread above suggests that there was a problem that may have been fixed in some versions, but this seems like a bug still exists.

    Does anyone know a workaround?
     
  29. electric_jesus

    electric_jesus

    Joined:
    Mar 8, 2015
    Posts:
    31
    I still have this bug on Android in Unity 2018.3. After setting VideoPlayer.time manually the audio goes out of sync.
     
  30. Gohaleg

    Gohaleg

    Joined:
    Sep 2, 2013
    Posts:
    7
    Echoing others: Still an issue on 2019.3.6
     
    roumenf likes this.
  31. JerryWitt

    JerryWitt

    Joined:
    Feb 15, 2019
    Posts:
    3
    I realize this discussion has revolved around AUDIO issues in VIDEO with seek. But I am having an issue with VIDEO playback after a seek. If I have a Quicktime video playing in a video player object and I seek backwards 10 seconds, the video freezes and invoking videoObject.Play(); does nothing. Anyone else having this issue?
     
    jonathaniscarroll likes this.
  32. jonathaniscarroll

    jonathaniscarroll

    Joined:
    Oct 6, 2015
    Posts:
    3
    I am experiencing this as well, did you figure anything out?
     
  33. XRA

    XRA

    Joined:
    Aug 26, 2010
    Posts:
    191
    same issue, seeking is buggy / not working, 2019.2, doubtful it will be fixed.

    There are free solutions, though they may not work for your intended target platform:

    https://github.com/adrenak/GPUVideoPlayer
     
    Last edited: Nov 9, 2019
    gonzolot likes this.
unityunity