infiniteloop infiniteloop - 3 years ago 245
iOS Question

For consistent tempo on iPhone, is there a better solution than system sounds?

I've been working on a metronome app for quite some time now. Following Apple'e sample Metronome application, I've been using a timer on a (high priority) background thread to play system sounds. My results are okay but far from perfect. Some issues that I face are:

  • the app doesn't really keep time, although it sounds like it does

  • I can't do much on the main thread while the sound thread is running as it gets really sluggish

  • If I press the lock button or volume buttons on the device, the tempo goes haywire.

  • system sounds don't play when the phone is silent

I purchased a metronome on the app store yesterday and found that this app didn't have any of the above issues. It wasn't using system sounds as muting the iPhone didn't stop playback.

Now, I'm wondering if anyone can guide me towards a better, more robust framework for this task?

Answer Source

The OSX audio libraries are referred to as "Core Audio". There are a few different ways of interacting with the audio hardware and the Core Audio frameworks. There's OpenAL (as mentioned in another comment) which is a cross-platform standard approach to audio much like OpenGL is for graphics. There is also Audio Unit which is a callback-based interface directly to the audio hardware which allows you to supply raw PCM audio in real-time. Audio Units are extremely time-constrained, so if you're looking for metronomic rhythm, Audio Units can do that for you. There's also a layer on top of Audio Units known as AudioQueue which allows you to schedule a stream of audio for play in real-time. One of the benefits of AudioQueue is that it can automatically decode compressed formats such as MP3 or AAC.

Search on "Core Audio Overview" in the docs to learn more about the various audio frameworks for iPhone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download