Sunday, May 31, 2009

AVAudioPlayer could not play after recording via AudioQueue Object.


I've been fighting for AVAudioPlayer and AudioQueue object for 2 days,

Here is my Dev environment:
iPhone: 3.0Beta 5
OSX 10.5.7

Here is my scenario:
1. I need to use AVAudioPlayer to play a CAF with IMA4 data format. Including fast forward and rewind.
2. I need to use AudioQueue Object to record audio, because 3.0 has not go public yet.
3. A common scenario is record an audio A, then play the original audio A'(Not the one recorded) or vice versa.

The problem is I could NOT play the A' file  after I record file A.
[theAVAudioPlayer play] always return NO, but [theAVAudioPlayer prepareToPlay] return YES.
This situation only happened after I used the AudioQueue Object for recording. If I did not record, it always worked fine.

About the clean up.
1. I released the theAVAudioPlayer object and set to nil each time after I finished playing.
2. I closed the audio file and deposed the AudioQueue Object each time after I finished recording.


Thanks to Vitali Molodtsov[Open the original post Needs login], the solution is reset the AudioSessionCategory to kAudioSessionCategory_MediaPlayback just after finish recording. Then every thing goes fine.


I think this is a bug for AVAudioPlayer. Each time, when we initialize an AVAudioPlayer object, it should set the correct AudioSessionCategory for us silently, but it doesn’t. So I’ve fired a bug for Apple.


  1. I really like your post. Does it copyright protected?

  2. Thanks so much for posting this hint about resetting the AudioSessionCategory. I've been pulling my hair out for the past day trying to get this to work.

    My next problem is that AVAudioPlayer (only on the device) does not appear to be able to playback ulaw encoded files. I'm still trying to find the best simple format for recording dictation. I need something that is easily played on Windows as well a Mac

  3. thanks!

    this post really helps!

  4. You you could edit the page subject title AVAudioPlayer could not play after recording via AudioQueue Object. Tonny Xu to something more better for your webpage you make. I enjoyed the blog post even sononetheless.