有没有人在使用 iPhone 5 时遇到过 AVAudioPlayer 的问题?我的代码在 iPhone 4、iPhone 4S 和 iPad(第 3 代)上运行正常,但现在随机无法在 iPhone 5 上运行。我看到了这个问题,但没有人解决实际问题:
AVAudioPlayer is not working in iPhone 5
NSURL *soundURL = [self urlForAlarmSong:songKey];
NSError *err;
self.audioAlert = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&err] autorelease];
[self.audioAlert prepareToPlay];
currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];
[self.audioAlert play];
audioAlert 是我在 header 中使用 (retain,nonatomic) 声明的 AVAudioPlayer。 err 变量每次都为 null,即使它不在 iPhone 5 上播放也是如此。此代码每次在 iPhone 4、iPhone 4S 和 iPad(第 3 代)上都能完美运行。
有没有人有什么想法??谢谢
最佳答案
-(void)viewDidLoad
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"click2" ofType:@"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if(error)
{
NSLog(@"error in audio player:%@",[error localizedDescription]);
}
else
{
audioPlayer.delegate=self;
[audioPlayer prepareToPlay];
}
}
按钮操作,
-(IBAction)SoundButton:(id)sender
{
[audioPlayer play];
}
在你的 .h 文件中,不要忘记导入
#import AVFoundation/AVFoundation.h
并添加一个委托(delegate)AVAudioPlayerDelegate
。
关于iphone - AVAudioPlayer 在 iPhone 5 上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13710304/