2013-04-13 45 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

我在哪裏放置東西? –