嘿,我正在设计一个 iphone 应用程序,它以编程方式将按钮加载到滚动视图中并播放声音。我已经让声音按顺序播放,这很棒,但是每当我尝试按下按钮时,它不会记录按钮按下,直到序列中的最后一个声音开始播放。这是我播放 AVAudioPlayer 的主视图控制器中的代码:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
for (menuCount = 0; menuCount < [ivrTree count]; menuCount++ ) {
currentScript = [ivrTree objectAtIndex: menuCount];
for (btnCount = 0; btnCount < [currentScript.menuChoices count]; btnCount = btnCount) {
currentChoice = [currentScript.menuChoices objectAtIndex: btnCount];
if (![thePlayer isPlaying]) {
if (!mute) {
[self playPromptAudiofromSRC:self src:currentChoice.audioSRC];
}
btnCount++;
}
}
}
thisMenuPlayed = true;
}
- (void)playPromptAudiofromSRC:(id)parent src:(NSString *)src {
NSURL *fileURL;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *audioPrompt = [NSString stringWithFormat:@"%@%@", [defaults stringForKey:CONFIG_IVR_HOST], src];
if ([src isEqualToString:@""] || src == nil) {
audioPrompt = @"silence1sec";
NSString *path = [[NSBundle mainBundle] pathForResource:audioPrompt ofType:@"wav"];
fileURL = [NSURL fileURLWithPath:path];
} else {
fileURL = [NSURL URLWithString:audioPrompt];
}
AudioServicesDisposeSystemSoundID (_audioPromptSound);
AudioServicesCreateSystemSoundID((CFURLRef)fileURL, &_audioPromptSound);
AudioServicesPlaySystemSound(_audioPromptSound);
//NSLog(@"Audio SRC: %@", audioPrompt);
NSData *soundData = [NSData dataWithContentsOfURL:fileURL];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:NULL];
self.thePlayer = player;
[player release];
[thePlayer prepareToPlay];
[thePlayer setVolume:1.0f];
[thePlayer play];
}
我不知道为什么这只会在整个播放后记录按钮按下。我正在尝试实现一个静音按钮和一种在音频播放时“插入”音频的方法。在此先感谢您的帮助