正确的方式添加一个声音到UIAlertView [英] Correct way to add a sound to UIAlertView
问题描述
向UIAlertView添加声音的最正确方法是什么?
我应该将其子类化,然后在视图出现时播放系统声音?
还是会更正确,不是子类,而是调用一个单独的方法来播放声音时出现?
任何人都可以推荐一个比UIAlertView更有声音和外观的控件。
c> AVAudioPlayer 下面的代码: -
在.h文件中设置 UIAlertView
的dalagete like: -
@interface ViewController:UIViewController< UIAlertViewDelegate>
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@%@ / foo.mp3,[[NSBundle mainBundle] resourcePath]]];
NSError * error;
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Titlemessage:@messagedelegate:self cancelButtonTitle:@OkotherButtonTitles: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];
}
}
EDIT
注意:不要忘记添加
AVAudio框架
如果你想播放声音直到没有按下UIAlertView
的Button,然后设置audioPlayer.numberOfLoops = -1;
p>What is the most correct way to add a sound to a UIAlertView?
Should I subclass it and then play a system sound when the view appears?
Or would it be more correct, not to subclass but instead call a separate method to play a sound when it appears?
Can anyone recommend a control that plays sound and looks prettier than UIAlertView?
解决方案you can achieve this by using
AVAudioPlayer
bellow code:-Set dalagete of
UIAlertView
in .h file like:-@interface ViewController : UIViewController <UIAlertViewDelegate>
in .m file
- (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]; } }
EDIT
NOTE:- Do not forget to Add
AVAudio framework
And if you wish to sound playing till not press Button ofUIAlertView
then setaudioPlayer.numberOfLoops = -1;
这篇关于正确的方式添加一个声音到UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!