Search Unity

Need a fix for Audio Lag (Android)

Discussion in 'Editor & General Support' started by markkerecz, Sep 10, 2014.

  1. markkerecz

    markkerecz

    Joined:
    Jul 30, 2012
    Posts:
    4
    I have a game in which the player can shoot his weapon by tapping the
    screen. This works fine while on the computer but when I test on my LG G2 I
    am experiencing an obvious delay between the touch and the audio sample
    playing.

    I've tried using an existing pool of audio sources so that I can iterate
    through them and change the audio clip to play.....as opposed to using
    PlayOneShot which I heard is not as efficient.

    All of the audio clips that are imported into my library are properly set
    according to the optimization guidelines. I am currently using Unity
    3.5.7f6 Pro version. I have set the Audio to Best Latency. I've even set
    the audioSource's time property to .4f to see if starting the audio clip
    further in would solve this problem.

    Hopefully somebody can shed some light onto this issue :)
     
  2. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    Your saying "properly set according to the optimization guidelines" probably means you've already thought of this, but...

    The first thing that comes to mind is decompression overhead before playing the sound. Is your original audio file an uncompressed WAV? If not, it should be.
     
  3. markkerecz

    markkerecz

    Joined:
    Jul 30, 2012
    Posts:
    4
    Yes, all of the files are uncompressed wav format.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,424
    I have the same experience, as do many others if you google. I have tried many things, settings, how I call it ect. There is just some issue with some android phones. Not all, but some are kooky.
     
  5. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    You can try setting a lower DSP Buffer Size. Smaller buffer means less latency.
     
  6. ChristopherCreates

    ChristopherCreates

    Joined:
    Apr 8, 2015
    Posts:
    20
    renman3000 likes this.
  7. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,019
    Your problem came from 2 things : Unity adds audio latency and ALSO input latency. Your sound is a result of tapping the screen. Audio latency is one thing but input latency also indirectly increase the perceived audio latency. (You can look at my research here : https://github.com/5argon/UnityiOSNativeAudio)

    To fix audio latency :
    Project Setting > Audio > DSP Buffer Size > set it to Best Latency (small buffer size). As of today with this settings, it make a glitched sound on Windows build while on macOS, Android, iOS is completely fine. You might want to have larger buffer size on Windows. (at the expense of more latency)

    If that is not enough you can use native methods of each platform. I just made Native Audio asset store plugins which can make a native call to both iOS and Android's fastest native way from one central interface. https://www.assetstore.unity3d.com/#!/content/107067

    There are various ways of playing audio at native side, here's my choice :

    - On iOS it uses OpenAL. It is faster than AVAudioPlayer, AudioToolbox, and SystemSound.
    - On Android it uses AudioTrack, I confirmed it to be faster than SoundPool and no meaningful difference from C++ side OpneSL ES of NDK.

    I have compiled all of my findings in here : http://exceed7.com/native-audio
    PS. I have used FMOD for Unity before. The best settings that I could do. In addition to setting the best file format, requires editing FMOD Unity's source code to use very low number of buffer size. With that still the latency is just about equal to Unity's "Best Latency" (ant the sound cracks more too due to a low buffer size)

    To fix input latency :
    This is much more difficult as the path that Unity receives touch from Xcode project is almost hardwired and is not meant to be replaced easily. (Unlike audio, we just left the Unity one and use our native method)

    I made iOS Native Touch which can reduce this input latency. But you will lose many conveniences that Unity provides including finger ID tracking, stationary state, etc.

    http://exceed7.com/ios-native-touch/
     
    TigerHix likes this.