Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question VideoPlayer resets to beginning upon RebuildGraph, but only in Cloud Builds.

Discussion in 'Audio & Video' started by JonathanBartel, May 4, 2021.

  1. JonathanBartel

    JonathanBartel

    Joined:
    Jun 24, 2019
    Posts:
    45
    This one's had me stumped for a few weeks and I'm now past my deadline because of it.

    We have a Video Script Playable Track in our timeline. The timeline can be manipulated at runtime (play, pause, step, rewind...) Occasionally, we need to update the timeline at runtime, so we call PlayableDirector.RebuildGraph(). When we do, the VideoPlayer resets to the beginning of the video instead of staying at its current frame/time. So I collect its frame prior to calling RebuildGraph(), then call RebuildGraph(), then set the time using VideoPlayer.time. This works great in the editor and it works with local builds to Windows, OSX, and iOS, but when I build using Unity Cloud Build, it no longer works. The video returns to its first frame.

    I've read every forum post I could find that seemed relevant, but none have solved my issue. Hoping someone can help, because I'm up against it. I'm running Unity Editor 2019.4.24f1 and Unity Cloud Build is set to match. Timeline package is version 1.5.4.

    Also, I apologize if this should be in the Cloud Build forum. This seemed like the right place since VideoPlayer has a reputation for finicky behaviour and Cloud Build is giving me no errors, but I can repost in the Cloud Build forum if you all think that's the issue.

    Here's my code:
    Code (CSharp):
    1.  
    2. private static IEnumerator RebuildGraph(Action callback)
    3.     {
    4.         double showTime = playableDirector.time;
    5.         // timelineControls is a custom class for interacting with timeline (play, pause, etc.)
    6.         bool paused = timelineControls.isPaused;
    7.  
    8.         // timelineObjects is a parent gameobject for the gameobjects controlled by the timeline
    9.         VideoPlayer[] vids = timelineObjects.GetComponentsInChildren<VideoPlayer>();
    10.         double[] currentTime = new double[vids.Length];
    11.  
    12.         // there is currently only one video, but could be more in the future
    13.         for (int i = 0; i < vids.Length; ++i)
    14.             currentTime[i] = vids[i].time;
    15.  
    16.         playableDirector.RebuildGraph();
    17.  
    18.         // RebuildGraph starts playing automatically.
    19.         // We want to stay paused if we were paused before rebuilding.
    20.         if (paused) timelineControls.SetAnims("pause");
    21.  
    22.         // first we need to return to the right time in the timeline
    23.         playbleDirector.time = showTime;
    24.  
    25.         // move all videos to the correct time
    26.         for (int i = 0; i < vids.Length; ++i)
    27.         {
    28.             // I read somewhere that the video needs to be prepared first
    29.             if (!vids[i].isPrepared)
    30.                 vids[i].Prepare();
    31.  
    32.             // should only take a frame or two
    33.             while (!vids[i].isPrepared)
    34.                 yield return null;
    35.  
    36.             // must be playing to set the time
    37.             vids[i].Play();
    38.             vids[i].time = currentTime[i];
    39.             yield return new WaitForSeconds(.1f);
    40.         }
    41.  
    42.         callback();
    43.     }
    44.  
     
    Last edited: May 4, 2021
  2. JonathanBartel

    JonathanBartel

    Joined:
    Jun 24, 2019
    Posts:
    45
    In true fashion, a day after posting to the forum, I discover the answer. This happens every time.

    For anyone else having this issue, it was a Unity Cloud Build setting. Mods can decide if they want to move this post to that forum.

    As I said, I'm running Unity Editor 2019.4.24f1, and in my Cloud Build settings, I had "Auto Detect Version" off and set Unity Version to "Always Use Latest 2019.x". Currently, the latest version of 2019.x is 2019.4.25f1, so I'm not far off, but apparently, that's where the problem was. I turned on "Auto Detect Version" and now I no longer have the issue. I don't get it, but there it is. o_O