Fix sound delay
Hello!
I'm trying to play sounds on my android device, however they're always delayed by about half a second. Why is that?
Answer by panoramabeats · Dec 06, 2016 at 12:28 AM
This is one setback of the Android OS in comparison to the iPhone/iOS all-inclusive atmosphere.
Because of the fact that there are simply so many different devices, the OS and the device are not always made to be one single cohesive item, if that makes any sense.
A Google search for 'Android audio kernel' will bring up most of the background information on the subject.
I'm not a fan of iPhone, but when it comes to audio latency in apps, it is actually superior.
This issue is mostly device-specific, but also has to do with processing. Are there background processes that can be eliminated.
How is your audio implemented in Unity? Are you using any middleware, etc.? An option you should select if possible is to pre-load the sample, instead of loading when encountered in the app.
Also, are you compressing your audio files to mp3 or an equivalent? Avoid using bulky WAV files, etc. if processing and latency are issues.
Hope this helps in some way.
Answer by 5argon · Apr 14, 2018 at 04:32 AM
What you could do is 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 "Best Latency" setting is still not enough for you going native is definitely the way to go. 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)
I was experiencing the same issue in a regular PC build (using .ogg files, not heavy .wav files). The DSP Buffer Size change fixed my problem. Great suggestion, thank you!
Answer by lloydg · Jan 13, 2017 at 08:44 AM
It is unity doing fancy stuff with the audio, this is slow on Android.
I used this plugin to play sounds faster, its a bit limited in features: https://forum.unity3d.com/threads/android-sound-latency-fix.319943/
Note that the link is broken in the original post, however, on page 2 I have uploaded the files.
Answer by LexH · Nov 23, 2018 at 10:20 AM
thanks a lot!!! Native Audio http://exceed7.com/native-audio/ 爬文許久 我還沒試過 但是 上面這個連結貌似可以 是目前我看到的最新解決方案
ps建議用英文搜尋 較冷僻的問題用中文搜尋不好找到
Answer by Alexander-Fedoseev · Dec 12, 2018 at 12:07 PM
I'm using Master Audio plugin on Unity. With a new game I've noticed about 500 ms latency on Samsung S8 which was very confusing since on some old devices that i have there were not noticable latency. DSP Buffer Size to low latency helped a little. Ignore time scale switcher in Master Audio helped a little. But the latency didn't disapiear. So I've applied Native Audio plugin for Android and it works very well, but there is no such power and flexibility compare to Master Audio. And the only available sound quality for now is 16-bit PCM 44100Hz Stereo .wav which is not good for me. So what helped me to reduce the latency to about 100 ms i guess and made it acceptable for me is:
private void PlaySoundAfterUpdate(string soundName)
{
StartCoroutine(PlaySoundAfterUpdateCoroutine(soundName));
}
private IEnumerator PlaySoundAfterUpdateCoroutine(string soundName)
{
yield return null;
MasterAudio.PlaySoundAndForget(soundName);
}
In most cases i ve tried to play sound in OnCollisionEnter2D or FixedUpdate. After I've started to wait until next Update the latency reduced dramaticly. Still not so low as in Native Audio, but acceptable for me. Hope it will help somebody.
Hello, alternatively for many Samsung devices + chinese phones you may try upgrading to 2019.1 alpha. It will instantly provide you less latency for non-native normal Unity audio playback. Please see my article here : https://gametorrahod.com/unitys-android-audio-latency-improvement-in-2019-1-0-ebcffc31a947
The reason your older devices got less latency is because of newer devices unfortunately not passing Unity's fast audio track criteria. These requirements were relaxed in 2019.1
Your answer
Follow this Question
Related Questions
Cracking at end of audio? 0 Answers
Need help: new sound isnt played correctly anymore 2 Answers
Second AudioClip won't play 0 Answers
How to prevent the audio clip from playing louder? 0 Answers