iOS系统提示音以及震动

    xiaoxiao2021-03-25  96

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

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

    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];//提示音

    最后,上述是音效和震动两者都有,如果需求只有其一的话,注释不需要的即可。
    转载请注明原文地址: https://ju.6miu.com/read-39953.html

    最新回复(0)