1

ユーザーが「OK」ボタンを押したときに停止したいサウンドループをアプリで作成しUIAlertViewました。

ただし、2つの問題があります。


初め

ブレークポイントをオンにして、すべての例外にブレークポイントを設定すると、例外が表示され[audioPlayer play]ますが、ログにエラーは表示されず、音が出ないことを除いて、例外を「F8-ing」してもアプリはクラッシュしません。


2番

私が抱えているもう1つの問題は、ユーザーが「OK」ボタンをタップした後にオーディオファイルが停止せず、ブレークポイントを介して[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

明らかな問題の 1 つは、UIView デリゲートとして nil を渡したために、コールバックされないことです。

すなわち

   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];

もう 1 つの問題は、buttonIndex がゼロから始まることです。したがって、コールバックでは、1 ではなく 0 をチェックする必要があります。

于 2012-12-30T20:28:06.403 に答える