Just published my Recordable Launchpad app on the iOS App Store and Google Play! Actually, the project was created accidentally when I was testing out Unity In-App Purchase, Google Admob Unity SDK, the “Prepare iOS for Recording” checkbox, and Audio Latency.
Since integrating Unity In-App Purchase and Google Admob are rather straightforward, I would be writing mostly about (the more troublesome parts, which are)
- Serializing and Deserializing
- Getting Rid of Audio Latency
- Prepare iOS for Recording and
in this article.
Get Rid of Audio Latency
When playing audio effects in Unity, there are always some 0.1-0.5 seconds of latency, which is extremely annoying. The problem is rather simple to fix, but I actually spent a lot of time searching for the best solution.
One method is to use .wav files instead of .mp3 because there would always be some overheads in decompressing .mp3 files. (However, .wav files are much larger…which is fine for short audio effects though)
The other method is really trivial, but it actually solved the problem for me. Just select Best Latency in Project Settings -> Audio -> DSP Buffer Size, and the job is done!
Nonetheless, some Android devices may still have some noticeable latency due to the variety of audio hardwares on different devices. The only solution to the problem may be writing native Android plugins.
Prepare iOS for Recording and iPhoneSpeakerFix
In iOS, the
Microphone.Start() call is always laggy. Fortunately, Unity provides a Prepare iOS for Recording checkbox in Player Settings. Though Prepare iOS for Recording solves the
Microphone.Start() latency, it generates another issue: All of the audio would come out from the small earphone on the top of the phone instead of the bottom speakers! This is really irritating since the volume would be extremely low, and the quality of audio would be very bad.
After days of searching, I finally found a nice solution on GitHub: UnitySpeakerFix. This little plugin written by cbaltzer is extremely simple to integrate. Just copy and paste the files and add
Microphone.Start() call, and boom! The audio would now come perfectly out of the speakers again!
In my Recordable Launchpad app, I saved the recorded
AudioClips in binary files. Whenever the app is started, the
AudioClips are deserialized from the local binary files, and when the app quits, the
AudioClips are serialized and saved in the local directory.
The most important points when serializing or deserializing
- Get new
AudioClipin Unity consists of three important fields:
- The actual audio samples are stored in a
floattyped array (
sample, where the length is
- Copy audio samples by the
So, firstly, we should create a serializable class to store
Then, the serialize and deserialize code should be quite simple:
Deserialize AudioClip from local file and set to AudioSource
Serialize AudioClip to local file
The full source code can be found on my GitHub