正确的方式添加一个声音到UIAlertView [英] Correct way to add a sound to UIAlertView

查看:86
本文介绍了正确的方式添加一个声音到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 of UIAlertView then set audioPlayer.numberOfLoops = -1;

这篇关于正确的方式添加一个声音到UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆