Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Working with video playback lag in Unity

Discussion in 'Editor & General Support' started by tex-murph, Jun 1, 2019.

  1. tex-murph

    tex-murph

    Joined:
    Mar 4, 2019
    Posts:
    3
    Hi! I've been learning Unity's video playback API, and am starting to wonder if I should switch to AVPro, or if there is a fix for my lag issues I'm encountering while switching videos. I'm working on creating an interactive FMV game with branching choices, so I need to be able to access numerous videos during gameplay. I can't just pre-load a small playlist of videos to play sequentially.
    Right now I'm generally okay with Unity's video playback, but even with some custom scripting, am unable to achieve the smooth performance in switching videos I'm getting out of AVPro.
    With Unity's stock tools, I'm still getting a few frames of black between switching videos. I'm using multiple video players and only switching players once the video is prepared, and am realizing that playback begins *after* "video.isPlaying" is true. There's this small varied delay of 1-2 frames that I can't seem to be able to account for. I tried manually adding a coroutine to make the player wait a few frames before switching, but it's far from perfect.
    It's not a huge thing, but I am seeing how AVPro switches videos seamlessly out of the box, and am wondering if I will see other kinds of benefits to using AVPro as I continue. The Unity workarounds I've encountered online don't seem to account for my situation (wanting to be able to load various videos during gameplay), but before switching to AVPro, I figured I'd ask here if anyone has come across a better fix for my situation?
    I have come across the idea of using 100+ video players you start and stop to seamlessly switch, but my understanding is this approach can run into performance issues depending on the platform. I'd like my game to be mobile friendly, and do see that AVPro handles mobile functionality very well.

    Sorry for the long post, hope that makes sense. For reference, here is my current code I'm using that combines various workarounds I've seen online.

    Code (CSharp):
    1.  void SwitchPlayers(VideoPlayer thisPlayer)
    2.     {
    3.         activePlayer = otherPlayer;
    4.         otherPlayer = thisPlayer;
    5.         activePlayer.targetCameraAlpha = 1f;
    6.         otherPlayer.targetCameraAlpha = 0f;
    7.  
    8.         Debug.Log("new clip: " + nextClip.name);
    9.         nextClip = null;
    10.     }
    11.  
    12.  
    13.  
    14.     IEnumerator playVideo()
    15.     {
    16.         // cue clips
    17.         nextClip = playlist[0];
    18.         otherPlayer.clip = nextClip;
    19.  
    20.         //Disable Play on Awake
    21.         otherPlayer.playOnAwake = false;
    22.  
    23.         // Prepare
    24.         otherPlayer.Prepare();
    25.  
    26.         //Wait until video is prepared
    27.         while (!otherPlayer.isPrepared)
    28.         {
    29.             Debug.Log("Preparing Video");
    30.             yield return null;
    31.         }
    32.  
    33.         print("isPrepared value is " + otherPlayer.isPrepared);
    34.         Debug.Log("Done Preparing Video");
    35.  
    36.         //Play Video
    37.         otherPlayer.Play();
    38.         Debug.Log("Playing Video");
    39.  
    40.         while (!otherPlayer.isPlaying)
    41.         {
    42.             print("Video is not playing yet");
    43.             yield return null;
    44.  
    45.         }
    46.  
    47.         //placeholder for where counter was
    48.  
    49.  
    50.         // Confirmed video is prepared and is playing. Time to switch.
    51.         SwitchPlayers(activePlayer);
    52.         otherPlayer.Stop();
    53.  
    54.         // Continue moving down the playlist
    55.         playlist.RemoveAt(0);
    56.  
    57.  
    58.     }
     
  2. TayannaStudios

    TayannaStudios

    Joined:
    Oct 18, 2013
    Posts:
    38
    I had this issue with Unity's native VideoPlayer. Have you transcoded the videos? This seems to get rid of the couple of black frames as it switches.
     
  3. Mehdi85

    Mehdi85

    Joined:
    Apr 23, 2020
    Posts:
    7
    Did you manage to figure this issue put? I have also messaged you, any help would be much appreciated.
     
  4. tex-murph

    tex-murph

    Joined:
    Mar 4, 2019
    Posts:
    3
    Since I've seen a few people reach out to me so far, here's a public response!

    For full disclosure, I never went beyond the prototype phase for my project, as I switched to a different project.
    That being said, I have a few suggestions
    - I would heavily recommend investing in the Unity asset AVPro that is popular with studios -

    it solves inherent problems in how Unity deals with video, and does in a very easy to use way. Switching between videos is much faster and optimized without having to code clunky workarounds that can take up more memory. Yes, workarounds exist, but they do not work well on all platforms such as mobile that have less memory.
    AVPro also solves issues regarding mobile ports. Porting to mobile can get tricky for various reasons - requiring different transcode settings, frame sizes, memory usage, etc, and AVPro streamlines that for you.
    - I also used the visual scripting tool Playmaker - - to map out my branching paths. Makes life a lot easier to visually see how all the choices connect.

    To be clear, you absolutely can use Unity to make an FMV game without buying any assets, but the time saved and quality of the results are worth it, IMO.

    You can also use Eko for interactive video -
    - The upside to Eko is that it hosts your videos for you, and works as a website, so it will work on any platform (mobile, etc). It has some basic scripting you can use to accomplish Bandersnatch type branching narrative. It is also free!
    - Downside is that it is nowhere near as powerful/flexible as Unity. You cannot create Her Story or Telling Lies in Eko, for example.
     
  5. nareshbishtasus

    nareshbishtasus

    Joined:
    Jun 11, 2018
    Posts:
    35
    Do not transcode the video and it will resolve the issue