UIAlertView にサウンドを追加する最も正しい方法は何ですか?
サブクラス化して、ビューが表示されたときにシステム サウンドを再生する必要がありますか?
それとも、サブクラス化するのではなく、代わりに別のメソッドを呼び出して、サウンドが表示されたときにサウンドを再生する方が正しいでしょうか?
サウンドを再生し、UIAlertView よりもきれいに見えるコントロールを推奨できる人はいますか?
UIAlertView にサウンドを追加する最も正しい方法は何ですか?
サブクラス化して、ビューが表示されたときにシステム サウンドを再生する必要がありますか?
それとも、サブクラス化するのではなく、代わりに別のメソッドを呼び出して、サウンドが表示されたときにサウンドを再生する方が正しいでしょうか?
サウンドを再生し、UIAlertView よりもきれいに見えるコントロールを推奨できる人はいますか?
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 を忘れないでください。UIAlertView
audioPlayer.numberOfLoops = -1;