【问题标题】:AVAudioPlayer and Button PressesAVAudioPlayer 和按钮按下
【发布时间】:2013-04-13 17:49:40
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios objective-c avaudioplayer


    【解决方案1】:

    编辑:

    添加委托:

    在您的 .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;
    }
    

    你能看出我在说什么吗?如果您启动乐器并检查正在发生的事情,我很确定您会看到活动高峰,直到所有声音都播放完后才会消退。 我认为这种方法的编写方式会锁定您的设备。希望对您有所帮助。

    【讨论】:

    • 在我调用 playPrompAudioFromSRC 方法之前我有一个 if ([thePlayer isPlaying]) ,所以它只会增加 btnCount 并在第一个音频完成后播放下一个音频。添加 [thePlayer stop] 没有帮助。也不知道你所说的错误处理是什么意思
    • 不,这两个我都没有。让我现在试试
    • 我应该把 东西放在哪里?
    • 它没有用 - 我把 和 thePlayer.delegate = self;
    • 我从来没有设置播放和静音 - 他们需要覆盖吗?我应该发布什么方法?顺便说一句,感谢您的时间和帮助
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2016-03-31
    • 2013-03-14
    相关资源
    最近更新 更多