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];
[zeroAudioPl开发者_C百科ayer prepareToPlay];
[zeroAudioPlayer play];
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.
You should use audiosession like this before you play:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
精彩评论