iOS系统提示音以及震动

写在前面:第一次写文,方便自己记忆,希望也能方便大家。

项目需求中,在经过特定的区域时应用会弹出对应的道路简图。在弹出图的同时需要提示用户,所以想到做成类似手机收到通知那样三全音和震动。贴出代码:

1.在.h中 申明一个加号方法创建为的是创建一个单例类,调用减号方法实现效果。

+ (PlaySoundAndShake *)shareinstencePlaySound;

- (void)playSound;

2.在.m中导入

#import <AudioToolbox/AudioToolbox.h>

static PlaySoundAndShake *_engine;

@implementation PlaySoundAndShake

+ (PlaySoundAndShake *)shareinstencePlaySound{

    @synchronized (self) {

        if (_engine == nil) {

            _engine = [[PlaySoundAndShake alloc]init];

        }

    }

    return _engine;

}

- (void)playSound{

    SystemSoundID bell;

    NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/sms-received1.caf"];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &bell);

    AudioServicesPlaySystemSound (bell);//三全音

    

    SystemSoundID wenwen;

    wenwen =  kSystemSoundID_Vibrate;//震动

    AudioServicesPlaySystemSound(wenwen);

}

3.调用

[[PlaySoundAndShake shareinstencePlaySound] playSound];//提示音

最后,上述是音效和震动两者都有,如果需求只有其一的话,注释不需要的即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值