AVAudioPlayer和Button Presses

时间:2013-04-13 17:49:40

标签: iphone ios objective-c avaudioplayer

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

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

1 个答案:

答案 0 :(得分:0)

编辑:

添加委托:

在你的.h文件中:

@interface ViewController : UIViewController<AVAudioPlayerDelegate>{
}

在您的.m文件中:

thePlayer.delegate = self;
[thePlayer play];

我可以看到你的animationDidStop可能存在问题,因为无论你做什么,它都会遍历所有声音。我会尝试逐行逐步解释它并解释我所追求的内容(因为我看不到你的整个代码会有一些猜测)。

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

你能看到我得到的东西吗?如果你启动乐器并检查发生了什么,我很确定你会看到一个活动的峰值,在所有声音播放之前不会消退。 我认为这种方法的编写方式会锁定你的设备。希望它有所帮助。