iOS 播放系统提示音和震动

准备工作:  

        首先应当引入系统库: AudioToolbox.framework . 

        引用: #import <AudioToolbox/AudioToolbox.h> 头文件.

1、播放系统自带的提示声

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  // 震动

 AudioServicesPlaySystemSound(1007); //这个声音是是类似于QQ声音的   


2、播放自定义的提示声,既有声音也带振动

- (void)playNotifySound {
    //获取路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
    //定义一个SystemSoundID
    SystemSoundID soundID;
    //判断路径是否存在
    if (path) {
        //创建一个音频文件的播放系统声音服务器
        OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID);
        //判断是否有错误
        if (error != kAudioServicesNoError) {
            NSLog(@"%d",(int)error);
        }
    }
    //播放声音和振动
    AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
        //播放成功回调
    });
}

3、只有声音不带振动

//必须得是自定义的声音,经过测试系统的声音好像都带振动
- (void)playNotifySound {
    //获取路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
    //定义一个带振动的SystemSoundID
    SystemSoundID soundID = 1000;
    //判断路径是否存在
    if (path) {
        //创建一个音频文件的播放系统声音服务器
        OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID);
        //判断是否有错误
        if (error != kAudioServicesNoError) {
            NSLog(@"%d",(int)error);
        }
    }
    //只播放声音,没振动
    AudioServicesPlaySystemSound(soundID);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值