实现微信摇一摇播放音效,代码如下:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"摇一摇,哈哈哈");
[self playSoundEffect:@"yaoyiyao"];
}
}
-(void)playSoundEffect:(NSString *)name{
//1.获得系统声音ID
SystemSoundID soundID;
/**
* inFileUrl:音频文件url
* outSystemSoundID:声音id(此函数会将音效文件加入到系统音频服务中并返回一个长整形ID)
*/
NSString *audioFile=[[NSBundle mainBundle] pathForResource:name ofType:@"m4r"];
NSURL *fileUrl=[NSURL fileURLWithPath:audioFile];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
//2.播放音频
AudioServicesPlaySystemSound(soundID);
//3.播放震动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
// AudioServicesPlayAlertSound(soundID);//播放音效并震动
}
注:这里的yaoyiyao是音效文件名,后缀名是.m4r,微信摇一摇的音效文件可以到网上下载。在模拟器中,也能像真机一样实现摇动手机,操作:Hardware-->Shake Gesture.
如果觉得《微信摇一摇插件ios_iOS摇一摇手机 播放微信摇一摇音效》对你有帮助,请点赞、收藏,并留下你的观点哦!