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

Video Freeze video on last frame

Discussion in 'Audio & Video' started by MarvellousLunatic, Jun 25, 2022.

  1. MarvellousLunatic

    MarvellousLunatic

    Joined:
    May 1, 2020
    Posts:
    11
    Greetings,

    I'm trying to figure out how I can pause a video on the last frame using Unity's VideoPlayer. At the moment it goes black after the last frame. In my mind this would be the usually behavior of video players. I tried something like this:

    private void PlayClip(Clip clip){
    ...
    videoPlayer.loopPointReached += VideoEndReached;
    }


    private void VideoEndReached(VideoPlayer videoPlayer)
    videoPlayer.frame = Convert.ToInt64(video.clip.frameCount) - 1;
    }

    But even than there is a short black-screen before it sets back to the last frame that I want to stay. I already asked google, but weirdly can't find the right thing. This seems like a obvious thing to do to me. I'm pretty sure I'm just missing out on a simple thing but can't pin point it out. Does someone else already had this 'problem'?

    Kind regards.
     
  2. MarvellousLunatic

    MarvellousLunatic

    Joined:
    May 1, 2020
    Posts:
    11
    I have a workaround in place at the moment. But I'm not sure how reliable it is:

    Code (CSharp):
    1. public VideoPlayer videoPlayer = null;
    2. private Coroutine waitForFinished;
    3.  
    4. public PlayClip(VideoClip clip, bool doLoop = true) {
    5.     ...
    6.     if (!doLoop)
    7.             {
    8.                 waitForFinished = StartCoroutine(WaitForFinished());
    9.             }
    10. }
    11.  
    12. IEnumerator WaitForFinished()
    13.         {
    14.             while (videoPlayer.frame < Convert.ToInt64(videoPlayer.clip.frameCount) - 1)
    15.             {
    16.                 {
    17.                     yield return null;
    18.                 }
    19.             }
    20.             if (!videoPlayer.isLooping)
    21.             {
    22.                 videoPlayer.Pause();
    23.                 videoPlayer.frame = Convert.ToInt64(videoPlayer.clip.frameCount);
    24.                 if (waitForFinished != null)
    25.                 {
    26.                     StopCoroutine(waitForFinished);
    27.                     waitForFinished = null;
    28.                 }
    29.             }
    30.         }
    Few things I don't understand why / or how they work:
    videoPlayer.frame < Convert.ToInt64(videoPlayer.clip.frameCount) - 1

    I'm not sure why I need to add the -1, otherwise I get a final blackscreen. From my understanding this would the frame before the last frame.
    videoPlayer.frame = Convert.ToInt64(videoPlayer.clip.frameCount);

    I'm not sure why this works and doesn't cause an out of bound exception.

    Well this would make sense if videoPlayer.frame isn't an index position, but this can't be right? Can it?
     
  3. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    If you set the looping to false, the VideoPlayer will pause on the last frame. Is there a reason you can't do that?
     
  4. MarvellousLunatic

    MarvellousLunatic

    Joined:
    May 1, 2020
    Posts:
    11
    Hi, thanks for the response.

    I'm not looping one shot clips but my code allows loops and one-shots. Sorry, this maybe is not the best code example. I tried to delete all unnecessary.
    For me it doesn't stop at the last frame. It goes black. It's not in the original clip, I checked that multiple times.

    So it is the default behavior it stops at the last frame? Mhmm, that's weird I'm pretty sure I have no other code that would manipulate the video player.

    I'll double check everything again.
     
  5. TayannaStudios

    TayannaStudios

    Joined:
    Oct 18, 2013
    Posts:
    38
    Same here; you're not alone.

    We're releasing our FMV game soon and this issue with the Video Player is a real grind for us.

    The Video Player has NEVER once held its last frame for us in the 2 or so years we've been using Unity, even with no additional code or anything, it simply doesn't do that for us.
     
  6. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    This is what I see on Unity 2021.3.7f1 on Windows. The video freeze on the last frame. I tested with Material Override, Render texture and Camera Far Plane.


    Can you check by any chance that there is no black frame at the end of your video? Otherwise, create an empty project, add a VideoPlayer and your clip and see if you can reproduce it. If you can reproduce it, report it as a bug, and give me the ticket number, I will try to have a look.
     
    Last edited: Nov 2, 2022
  7. TayannaStudios

    TayannaStudios

    Joined:
    Oct 18, 2013
    Posts:
    38
    No black frame; it just ends all videos when they reach the end for me. Unity 2021.3.13f1. I'll report it as a bug soon.
     
    The_Island likes this.