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

Question VideoPlayer stops after setting .frame or .time, .Play() does nothing, .isPlaying always true)

Discussion in 'Scripting' started by Ascanio1980, Oct 24, 2021.

  1. Ascanio1980

    Ascanio1980

    Joined:
    Jun 7, 2019
    Posts:
    51
    Unity 2020.3.8f1, have set up an Empty with a VideoPlayer component that renders to a texture, texture is then shown in a RawImage on a Canvas.

    Video is set to automatically start playing.
    I need to keep the video synched with a datalog that I playback in realtime, so I check for a gap between datalog playback and video playback, and want to set the video playhead where it needs to be.

    If I simply avoid interacting with the video by code (only reading from it), all is good and dandy.

    If I try to set either VideoPlayer.frame or VideoPlayer.time, the video stops (both visual and audio).
    VideoPlayer.Play() won't do anything.
    VideoPlayer.isPlaying will keep returning true, even if it's a horrible lie.

    Any thoughts on how to fix this?
     
  2. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Are you sure that the time / frame is available in the video?

    You may also need to call Pause first then set the frame/time.

    I would try disabling the automatically start playing, you call Play manually, maybe after you set the frame/time.

    The video player definitely has a few strange quirks.
     
  3. Ascanio1980

    Ascanio1980

    Joined:
    Jun 7, 2019
    Posts:
    51
    Thanks for the reply.
    Yes I am setting either an existing frame or a time within the duration of the video.
    I have tried using .Pause() then setting either frame or time, then calling .Play(). In this case the video will move to the requested point but will still not play.

    I will try doing away with autoplay.

    Is there a way to go around the arguably very buggy video player to… play a video?
     
  4. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    There are some events you can hook into as well, like if the video ends and if it is preparing. You may want to check if the video is prepared first, then do your frame manipulations after its prepared:

    Code (csharp):
    1.  
    2.  
    3. private void Start(){
    4.  
    5. VideoPlayer vp = GetComponent<VideoPlayer>();
    6. vp.loopPointReached += CheckOver;
    7. vp.Prepare();
    8. vp.prepareCompleted += CheckPrepared;
    9.  
    10. StartCoroutine(StartVideo(vp));
    11. }
    12.  
    13. private IEnumerator StartVideo(VideoPlayer target){
    14.  
    15.         while(!target.isPrepared){
    16.            // Debug.Log("loading / buffering");
    17.             }
    18.             yield return null;
    19.         }
    20.  
    21.         target.Play();
    22.  
    23.     }
    24.  
    25. private void CheckPrepared(VideoPlayer vp){
    26.         vp.prepareCompleted -= CheckPrepared;
    27.         //Debug.Log("<color=yellow>" + vp.clip.name + "</color>" + " is prepared!");
    28.     }
    29.  
    30. private void CheckOver(VideoPlayer vp){
    31.         vp.loopPointReached -= CheckOver;
    32.         //Debug.Log("video has ended");
    33.     }
    34.  
    Are your videos coming from a URL?
     
  5. Ascanio1980

    Ascanio1980

    Joined:
    Jun 7, 2019
    Posts:
    51
    I'll answer your points:
    - I do check whether the video is Prepared before setting either frame or time.
    - I put the video file in the Assets folder and linked it to the Video Player component in the Inspector directly.
     
  6. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    You may have to also set the VideoPlayer component bools to true for Wait For First Frame and Skip On Drop. That is ringing a bell for me too. I did a client job a few years ago that basically spins up video players on the fly and keeps them all synced up with audio and video, so it's been a while since I dove into the inner workings, but I definitely remember having some major growing pains regarding the video player component.

    Also you may have to type cast the time/frame values. I think time should be a double and frame should be a long
     
    Last edited: Oct 24, 2021