Iphone – Why does iPod’s audio not fade back in after the app caused it to fade out


My app needs to play some audio files and I want to fade-out the iPod sound, play my file, and then as soon as my audio stops playing fade-in iPod again (just like iPhone handles iPod music when an incoming call comes in)

I'm setting up my app's Audio Session to be kAudioSessionCategory_MediaPlayback and then just before I play my file I call AudioSessionSetActive (true)
then when my file ends playing I call AudioSessionSetActive (false)

What happens is that the iPod audio fades-out with the AudioSessionSetActive (true) call, but it never fades-in again after I call AudioSessionSetActive (false)

I'm using AVFoundation class to play my audio, I audioPlayerDidFinishPlaying in my delegate method and that's where I call AudioSessionSetActive (false).

What am I missing here? Why doesn't the iPod fade-in when I deactive my audio session? Do I have the wrong Audio Session Category for this?

Best Solution

Ok, this is lovely. In iPhone OS3, see AddMusic example.

In essence, you do this:

#import <MediaPlayer/MediaPlayer.h>
[[MPMusicPlayerController iPodMusicPlayer] pause]
...(play your audio, with AmbientSound category)
[[MPMusicPlayerController iPodMusicPlayer] play]

This is basic. You can also add fadeout by looping volume change etc.

Related Question