1

UIAlertView にサウンドを追加する最も正しい方法は何ですか?

サブクラス化して、ビューが表示されたときにシステム サウンドを再生する必要がありますか?

それとも、サブクラス化するのではなく、代わりに別のメソッドを呼び出して、サウンドが表示されたときにサウンドを再生する方が正しいでしょうか?

サウンドを再生し、UIAlertView よりもきれいに見えるコントロールを推奨できる人はいますか?

4

1 に答える 1

4

AVAudioPlayer以下のコードを使用してこれを実現できます:-

UIAlertView次のように .h ファイルの dalagete を設定します。

@interface ViewController : UIViewController <UIAlertViewDelegate>

.m ファイルで

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/foo.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 1;
    [audioPlayer play];
    [alert show]; 
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [audioPlayer stop];

    }

}

編集

注: -ボタンを押してから設定AVAudio frameworkするまでサウンドを鳴らしたい場合は、Add And を忘れないでください。UIAlertViewaudioPlayer.numberOfLoops = -1;

于 2013-08-10T08:57:02.623 に答える