2013-04-13 44 views
0

嘿,我正在设计一个iPhone应用程序,编程方式将按钮加载到滚动视图和播放声音。我已经听到了按顺序播放的声音,这很棒,但每当我尝试按下按钮时,它都不会注册按钮按钮,直到序列中最后一个声音开始播放。下面是我的主视图控制器的代码,我玩AVAudioPlayer: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]; 
} 

我不知道为什么整个事情播放完毕后这只寄存器按下按钮。我试图实现一个静音按钮和一种方式来在播放音频时“插入”音频。在此先感谢您的帮助

回答

0

编辑:

添加委托:

在您的.h文件中:

@interface ViewController : UIViewController<AVAudioPlayerDelegate>{ 
} 

在您.m文件:

thePlayer.delegate = self; 
[thePlayer play]; 

正如我所看到的,您的animationDidStop可能存在问题,因为它将遍历所有声音而不管w你做的帽子。我会尝试逐行通过它并解释我之后的事情(因为我看不到你的整个代码,所以会有一些猜测)。

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { 
// Iterate through array ivrTree elements. 
for (menuCount = 0; menuCount < [ivrTree count]; menuCount++) { 

    // Starting with currentScript = ivrTree objectAtIndex 0 
    currentScript = [ivrTree objectAtIndex: menuCount]; 

    //Iterate through menuChoices, Starting with objectAtIndex 0 
    for (btnCount = 0; btnCount < [currentScript.menuChoices count]; btnCount = btnCount) { 
     //Set currentChoice to menuChoices objectAtIndex 0 
      currentChoice = [currentScript.menuChoices objectAtIndex: btnCount]; 
     //Will try to run playPromptAudiofromSRC if thePlayer isn't playing. Will 1st run evaluate as false and therefor will play. 
      if (![thePlayer isPlaying]) { 
       if (!mute) { 
        [self playPromptAudiofromSRC:self src:currentChoice.audioSRC]; 
       } 
     //Will increment btnCount to 1 when the audio has played through. 
      btnCount++; 
      } 
     //If sound hasn't finished playing it will stay in the for loop and keep trying until first sound is done playing. 
    } 
//Will increment menuCount 1 and start the next for loop all over again. 
} 
thisMenuPlayed = true; 
} 

你能看到我在做什么吗?如果你启动仪器并检查发生了什么,我很肯定你会看到活动中的高峰,直到所有声音都播放完毕才会消失。 我认为这种方法写的方式会锁定您的设备。希望能帮助到你。

+0

嗯,在我调用playPrompAudioFromSRC方法之前,我有一个if([thePlayer isPlaying]),因此它只会增加btnCount并在第一个音频完成后播放下一个音频。添加[thePlayer stop]没有帮助。不知道你的意思是错误处理 –

+0

不,我没有这些。现在让我试试 –

+0

我在哪里放置东西? –