系统不一致,震动可能就不存在了
1.引入库
#import <AudioToolbox/AudioToolbox.h>
2.iphone6震动可大了 iphone7 没有震动
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
3.必须在iphone7或者iphone7以上才有
/**
UIImpactFeedbackStyleLight,
UIImpactFeedbackStyleMedium,
UIImpactFeedbackStyleHeavy,
UIImpactFeedbackStyleSoft API_AVAILABLE(ios(13.0)),
UIImpactFeedbackStyleRigid API_AVAILABLE(ios(13.0))
*/
if (@available(iOS 11.0, *)) {
UIImpactFeedbackGenerator *fff = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleHeavy];
//[fff prepare];//可有可无
[fff impactOccurred];
} else {
}
4.自定义声音
- (void)method1
{
// 1、获取音效文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"begin.mp3" ofType:nil];
// 2、创建音效文件 URL
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
// 3、音效声音的唯一标示 soundID
SystemSoundID soundID = 0;
/**
* inFileUrl: 音频文件 url
* outSystemSoundID: 声音 id(此函数会将音效文件加入到系统音频服务中并返回一个长整形ID)
*/
// 4、将音效加入到系统音效服务中,NSURL 需要桥接成 CFURLRef,会返回一个长整形 soundID,用来做音效的唯一标示
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
// iphone6 有震动的 iphon7没有震动了
// 6、播放音频
//AudioServicesPlaySystemSound(soundID);
// 7、播放音效并震动
AudioServicesPlayAlertSound(soundID);
soundCompleteCallback(soundID, nil);
self.soundId = soundID;
NSLog(@"%u",(unsigned int)soundID);
}
- (void)stopAlertSound
{
AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);
AudioServicesDisposeSystemSoundID(self.soundId);
AudioServicesRemoveSystemSoundCompletion(self.soundId);
}
void soundCompleteCallback(SystemSoundID soundID, void *clientData) {
NSLog(@"播放完成");
AudioServicesRemoveSystemSoundCompletion (soundID);
// UILabel *label = (__bridge UILabel *)data;
// label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
网友评论