1

我在我的应用程序中制作了一个声音循环,当用户按下出现的“确定”按钮时,我想停止UIAlertView它。

我有两个问题:


第一的

当我打开断点并为所有异常设置断点时,会出现异常[audioPlayer play]但日志上没有显示错误,并且应用程序不会在通过异常“F8-ing”后崩溃,除非没有声音。


第二

我遇到的另一个问题是用户点击“确定”按钮后音频文件不会停止,并且断点显示它确实读取了[audioPlayer stop]调用。我不知道是什么导致了这些错误,我所做的似乎无济于事。


代码

AVAudioPlayer *audioPlayer;


-(void)done {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Timer Done" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alert show];

    [self playAlert];
}

-(void)playAlert {
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    audioPlayer.numberOfLoops = -1; //infinite

    [audioPlayer play];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        if ([audioPlayer isPlaying]) {
            [audioPlayer stop];
        }
    }
}

请让我知道我能做些什么来解决这个问题。

4

1 回答 1

1

一个明显的问题是您将 nil 作为 UIView 委托传递,因此您将永远不会被回调。

IE

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Timer Done" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];

应该:

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Timer Done" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];

另一个问题是 buttonIndex 从零开始。所以在回调中你应该检查零而不是一。

于 2012-12-30T20:28:06.403 回答