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 【WebGL】VideoPlayer sound and video are out of sync

Discussion in 'Audio & Video' started by hayakawa_0829, Mar 15, 2023.

  1. hayakawa_0829

    hayakawa_0829

    Joined:
    Dec 2, 2020
    Posts:
    2
    We are using Unity 2021.3.16f1 and the platform is WebGL.
    The mp4 on the server is played using VideoPlayer.
    As the title suggests, the sound and video are out of sync.
    Specifically, while the video is frozen, the audio does not stop, only the audio precedes it.
    VideoPlayer.time is incremented with the audio, even if the video is frozen.
    This problem occurs only with WebGL.
    The VideoPlayer configuration is as follows

    Code (CSharp):
    1.  private VideoPlayer CreateVideoPlayer()
    2.         {
    3.             videoPlayerObject = new GameObject("VideoPlayerObject");
    4.             videoPlayerObject.transform.parent = this.transform;
    5.             videoPlayerObject.transform.position = Vector3.zero;
    6.             var vp = videoPlayerObject.AddComponent<VideoPlayer>();
    7.             VideoPlayerSetting(vp);
    8.             return vp;
    9.         }
    10.  
    11.         private void VideoPlayerSetting(VideoPlayer vp)
    12.         {
    13.             vp.prepareCompleted += OnPrepareCompleted;
    14.             vp.renderMode = VideoRenderMode.MaterialOverride;
    15.             vp.targetMaterialRenderer = screenRenderer;
    16.             vp.playOnAwake = true;
    17.             vp.isLooping = false;
    18.             vp.waitForFirstFrame = true;
    19.             vp.skipOnDrop = false;
    20.             vp.source = VideoSource.Url;
    21.             vp.audioOutputMode = VideoAudioOutputMode.Direct;
    22.         }
    23.  
    Is there a solution?
     
    Last edited: Mar 16, 2023
  2. hayakawa_0829

    hayakawa_0829

    Joined:
    Dec 2, 2020
    Posts:
    2
    The URL is erased.
    The VideoPlayer in the inspector looks like this
     

    Attached Files:

  3. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    It feels like the clock continues progressing even though it can't keep up with the video. This could happen if skipOnDrop is true. I see you set it to false in your code, but in the inspector, it is true. Can you check if it is really false? Also, can you try to upgrade to the latest 2021? It could maybe fix your issue.
     
  4. Ohyouknow

    Ohyouknow

    Joined:
    Oct 23, 2013
    Posts:
    121
    Hi, @The_Island Is there a way to add a buffer or loading wheel to let the video catch up to the audio before resuming? I have a 10 second forward and backward button as well as the video slider and sometimes it'll freeze the video while the audio continues to play correctly, and then after a few seconds the video catches back up. I'm on 2020.3.38f1
     
  5. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    Seeking can take more than one frame. The best way to know when the VideoPlayer is done is to listen to frameReady. When you press the move forward button.
    1. Subscribe to frameReady and enable sendFrameReadyEvents to be true
    2. Display the loading wheel in your UI.
    3. Pause the audio.
    4. When you get the callback sync the audio to the video time.
    5. Hide the loading wheel.
    6. Start the audio.
     
    Last edited: Jul 19, 2023
    Ohyouknow likes this.