开发者

AVAudioPlayer plays music in iPod touch 2G and 3G

开发者 https://www.devze.com 2023-01-17 09:38 出处:网络
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'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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号