Search code examples
iosavaudioplayeripod-touch

AVAudioPlayer plays music in iPod touch 2G and 3G


I'm trying to play an audio file on iPhone and iPod touch with AVAudioPlayer. The code works fine in iPhone and iPod touch 4G, I can hear the music. But when I test it on iPod touch 2G, iPod touch 3G, I can't hear anything.

Below is the code I use to play audio file:

NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:@"TimerBell"
                                                          ofType:@"aif"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
                                                                    error:nil];
[file release];

self.zeroAudioPlayer = audioPlayer;
zeroAudioPlayer.delegate = self;
[audioPlayer release];
[zeroAudioPlayer prepareToPlay];
[zeroAudioPlayer play];

Solution

  • I found that changing from kAudioSessionCategory_PlayAndRecord to kAudioSessionCategory_AmbientSound corrected this issue. I suspect this has something to do with a hardware difference on the iPod touch related to recording.