当使用 AVAudioPlayer 按下按钮时,我正在尝试播放歌曲剪辑。整个剪辑在 Xcode 中播放良好,但是当我在设备上加载应用程序并按下按钮时,它会播放歌曲的第一秒,然后安静下来。
我在 viewDidLoad 下有这个:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Song.aiff", [[NSBundle mainBundle] resourcePath]]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&audioFileError];
player.numberOfLoops = -1;
然后当按下按钮时:
if (player == nil)
NSLog(@"Audio file could not be played");
else
[player play];
编辑:我也对如何播放音频剪辑的其他建议持开放态度,AVAudioPlayer 似乎是最简单和最直接的方法。