带声音的 IOS 警报消息 [英] IOS alert message with sound

查看:30
本文介绍了带声音的 IOS 警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何实现带有声音的警报消息?我基本上希望它在消息打开和关闭时重复使用默认的 iOS 警报噪音.

Does anyone know how to implement an alert message with sound? I essentially want it to use a default iOS alert noise repeatedly while the message is up and off when dismissed.

现在我已经实现了这样的消息:

Right now I have the message implemented as such:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert Message" 
                                                message:@"Noise should be playing until you push 'OK'!" 
                                               delegate:nil 
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

推荐答案

试试这个:

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

    }
}

来源:https://stackoverflow.com/a/18160299/3202193

这篇关于带声音的 IOS 警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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