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 Huge Android time lag on setting VideoPlayer/AudioSource to mute or volume to 0? Solution?

Discussion in 'Audio & Video' started by mikejm_, Sep 3, 2023.

  1. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    I am using VideoPlayer playback and need responsive mute/unmute functions.

    I notice that while in Windows Editor, VideoPlayer.SetDirectAudioMute, VideoPlayer.SetDirectAudioVolume, and AudioSource.mute all seem very rapidly responsive.

    However, when I build to Android mobile, although the VideoPlayer video playback seems good, the mute or volume control used by any of these methods is abysmal with at least 1/4-1/3 second lag.

    I tried setting the Player Audio settings to "Best Latency" but with no appreciable change. Is this due to massive Android mobile audio buffer sizes or what?

    Is there any way to do this another way? I tried doing the following on the GameObject with the AudioSource (when i tested the VideoPlayers to output to AudioSource in case that might help):

    Code (csharp):
    1.     void OnAudioFilterRead(float[] data, int channels) {
    2.         if (tempMute) {
    3.             for (int i = 0; i < data.Length; i++) {
    4.                 data[i] *= 0f;
    5.             }
    6.         }
    7.     }
    But it doesn't seem to be getting the audio or doing anything to mute it.

    Any thoughts?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Did you check the profiler already?
    Is this mono or il2cpp?
     
  3. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    Thanks for your input. I have been able to narrow down the following:

    - Buffer size on Android is same as on Editor (512) which is totally reasonable and should not explain the lag.
    - Using a custom mute by overwriting OnFilterRead with zeros (script must run as last element on game object) gives the same lag on Android as VideoPlayer.SetDirectAudioMute, VideoPlayer.SetDirectAudioVolume, and AudioSource.mute.
    - I have built to Mono (faster during development) but tried building to IL2CPP and same delay.

    I am using web streamed MP4 set to VideoPlayer by URL. I am now wondering if perhaps this is some sort of transcoding or other delay to the video processing done with Unity in Android but not Windows.

    Running ffprobe from ffmpeg I get for a few of my test videos:

    Code (csharp):
    1. Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 406x720 [SAR 1:1 DAR 203:360], 398 kb/s, 30 fps, 30 tbr, 15360 tbn (default)
    2.  
    3. Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 576x1024 [SAR 1:1 DAR 9:16], 1365 kb/s, 30 fps, 30 tbr, 15360 tbn (default)
    4.  
    5. Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/bt709, progressive), 406x720 [SAR 1:1 DAR 203:360], 341 kb/s, 30 fps, 30 tbr, 15360 tbn (default)
    These are just test videos downloaded from YouTube. In reality I can encode my videos to anything and will need to pick a codec with best compatibility to Android/iOS (likely VP8 due to no royalties like H264).

    If this is possibly the issue, is there any format I should try encoding my videos to that might work fluidly and without such a lag in both Android & iOS ideally and possibly Windows as well?
     
    Last edited: Sep 4, 2023
  4. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    I spent a few hours and submitted a bug report today as it was also reproduced when using the videos locally within the project via Resources.Load and without transcoding. I suspect something is not working correctly in Android or otherwise perhaps there is an Android specific workaround to fix it.

    Bug: IN-53846

    Responsive mute/unmute is needed for my purposes, like present as expected in Editor.
     
    The_Island likes this.