我正在使用此功能在我的 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");
}
}
}