2

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];

2 Answers 2

1

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.

1

You should use audiosession like this before you play:

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.