0

嘿,我正在设计一个 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];
}

我不知道为什么这只会在整个播​​放后记录按钮按下。我正在尝试实现一个静音按钮和一种在音频播放时“插入”音频的方法。在此先感谢您的帮助

4

1 回答 1

0

Edit:

Adding a delegate:

In your .h file:

@interface ViewController : UIViewController<AVAudioPlayerDelegate>{
}

In your .m file:

thePlayer.delegate = self;
[thePlayer play];

As I can see there could be a problem in your animationDidStop, as it will iterate through all sounds regardless of what you do. I'll try and step through it line by line and explain what I'm after(and there will be some guesswork as I can't see your entire code).

- (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;
}

Can you see what I'm getting at? If you start instruments and check what's going on I'm pretty sure you will see a spike in activity that won't subside until all sounds has played through. I think that the way this method is written it locks up your device. Hope it helps.

于 2013-04-13T18:08:50.937 回答