Search Unity

Lag on iOS when calling Microphone.Start()

Discussion in 'Audio & Video' started by casd82, Jan 12, 2017.

  1. casd82

    casd82

    Joined:
    Aug 29, 2015
    Posts:
    3
    I'm developing a loop station kind of app which the user could tap on a slot to record his/her voice and tap again to replay their voice.

    However, whenever I tap on the slot to record, there's a 0.5 sec lag... (It works fine on Unity Editor, but there's always a 0.5 sec lag on my iOS build)

    below is my code:

    Code (CSharp):
    1.  
    2. public void OnPointerDown()
    3. {  
    4.     if (recordMode) {
    5.         StartCoroutine(RecordCoroutine());
    6.     } else {
    7.         GetComponent<AudioSource>().Play();
    8.     }
    9. }
    10.  
    11. IEnumerator RecordCoroutine()
    12. {
    13.     //change the color to show that it's currently recording
    14.     GetComponent<Image>().color = Color.red;
    15.  
    16.      //start recording
    17.     GetComponent<AudioSource>().clip = Microphone.Start(null, false, 1, 44100);
    18.  
    19.     yield return new WaitForSeconds(1f);
    20.    
    21.     //stop the mic
    22.     Microphone.End(null);
    23.  
    24.     //recover button color
    25.     GetComponent<Image>().color = Color.white;
    26. }
    Is there a way to get rid of the lag... I've been searching for a solution for an entire week but in vain...

    P.S. I've already changed the DSP Buffer Size to Best latency to minimize the lag to play the audio clip.
     
  2. Matti-Jokipii

    Matti-Jokipii

    Joined:
    Apr 24, 2015
    Posts:
    16
    I think i've been seeing something similar. Sometimes Unity microphone seems to add a 0.5-1s latency for no apparent reason. The worst is that it's not consistent. Sometimes there is no extra latency. I've been debugging this and haven't really found anything aside from that it seems to occur inside Unity and none of my attempts to change the microphone configuration and initialization seems to eliminate the problem.
     
  3. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    no one have yet solved the problem?
     
  4. Cloudless-Rain

    Cloudless-Rain

    Joined:
    Jun 24, 2015
    Posts:
    12
    IMO this is a classic Unity problem. I've worked on this several times and still haven't found a solution. I don't get it, my only thought is that Unity just doesn't take live audio seriously.
     
    aihodge likes this.
  5. EricZinda

    EricZinda

    Joined:
    Jul 31, 2019
    Posts:
    2
    If the lag you are referring to is caused by Microphone.Start(), I hit that problem too, on iOS, with a noticeable .5ish second lag before Unity starts recording. I was able to fix by setting the "Prepare iOS for Recording" Player setting to true. This was in Unity 2210.3.24f1