November 9, 2011

Xcode: Some thoughts on using audio

Sooner or later, you will want to have some sound effects or music in your iphone app.

I started audio experimenting using the AVAudioPlayer class (AVFoundation framework), but later tried using System Sound Services (AudioToolbox framework) too. Had some thoughts after trying out both which I will be sharing in this post.

AVAudioPlayer
  • has an initial slight delay before the audio plays (although I only observed this only in the simulator, not for the real hardware)
  • allows audio looping
  • play multiple audio, one per AVAudioPlayer
  • able to play audio of any duration
  • independent of the phone's ringer volume, volume control will only affect AVAudioPlayer's audio during application run
  • much shorter code to implement (compared to System Sound)



SystemSoundServices
  • no visible delay before the audio plays (both simulator and real hardware)
  • no audio looping
  • no multiple audio, only one audio at a time
  • limited to short audio (30 seconds or less)
  • initial audio volume dependent on the phone's current ringer volume
  • more code to implement (compared to AVAudioPlayer)

So which one should you use?

If you just want to run a looping background music without any care for slight delay during launch of audio, then use AVAudioPlayer.

If you just want to play a short sound (eg knife swinging, buzzer sound etc) that does not require constant looping, then use SystemSoundServices.

I will update with some code examples at a later date...



1 comment:

  1. Hi,
    I wanted to ask you a question.
    In my project I`ve collision detection and after each collision appears a number. I`d like to add an audio for every number but what I`ve already did works bad.The numbers appears in a right way 1,2,3,4... but the sound not eg. four, four, one,two, two...
    Do you know why? Please could you help me?
    Aneta

    ReplyDelete