我正在使用这个函数在我的iPhone应用程序上播放声音。问题是声音没有播放。该函数正常工作并正确读取文件路径,我没有收到任何错误信息,但声音没有播放。
-(void) playSound : (NSString *) fName; {
if ([audioPlayer isPlaying]) {
[audioPlayer stop];
}
audioPlayer.volume = 1.0;
soundpath = [NSString stringWithFormat:@"sounds/%@", fName];
soundFilePath = [[NSBundle mainBundle] pathForResource:soundpath ofType: @"mp3"];
soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil){
}
else {
[audioPlayer play];
if ([audioPlayer isPlaying]) {
NSLog(@"%@", @"run");
}
}
}