我正在使用此代码加载并开始播放声音:
- (void) buzz {
if (buzzSound == nil) {
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSError *error;
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *soundFile = [[NSURL alloc] initFileURLWithPath: [mainBundle pathForResource: @"buzzer_8"
ofType: @"wav"]];
buzzSound = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFile error: &error];
buzzSound.delegate = self;
if (DEBUG_BLE && error != nil)
printf("Error loading sound: %s\n", [[error localizedDescription] cStringUsingEncoding: NSUTF8StringEncoding]);
}
// Play the sound.
buzzSound.numberOfLoops = -1;
[buzzSound prepareToPlay];
[buzzSound setVolume: 1.0];
if (![buzzSound play]) {
self.buzzSound = nil;
[self buzzSound];
}
}
此代码由按钮上的触碰事件触发。当按钮获得触摸事件(打开或关闭按钮)时,它会停止声音:
[buzzSound stop];
嗡嗡声本身就是一个简短的.wav文件。
第一次调用嗡嗡声时,声音播放一次。它不重复。在后续调用中,它会重复。为什么呢?
间歇性地,如果我播放声音的时间与通过声音文件的一个周期的时间长度大致相同,则后续呼叫蜂鸣声时声音失败,播放返回NO。此后,播放将始终返回NO,直到重新加载声音,之后如上所述播放声音一次,然后开始正常工作,直到声音再次播放一小段时间。
我有audioPlayerDecodeErrorDidOccur:错误:设置;没有报告错误。