调用系统的提示音和振动

调用系统的提示音和振动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//        soundId = kSystemSoundID_Vibrate;//振动
 
- (id)initSytemShake{
    self = [super init];
    if (self) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//        soundId = kSystemSoundID_Vibrate;//振动
    }
    return self;
}
 
- (id)initSytemSoundWithName:(NSString *)soundName soundType:(NSString *)soundType{
    self = [super init];
    if (self) {
        NSString * path = [NSString stringWithFormat:@"System/Library/Audio/UISounds/%@.%@",soundName,soundType];
//        NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:soundName ofType:soundType]; //得到苹果框架资源
//        [[NSBundle mainBundle] URLForResource:@"tap" withExtension:@"aif"];//获取自定义的声音
        if (path) {
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundId);
            if (error != kAudioServicesNoError) {// 获取的声音的时候,出现错误
                soundId = 0;
            }
            
        }
    }
    return self;
}
 
- (void)play{
    AudioServicesPlaySystemSound(soundId);
}
 
- (void)free{
    AudioServicesDisposeSystemSoundID(soundId);
}
原文地址:https://www.cnblogs.com/-ios/p/6088563.html