1.往项目中导入AudiToolbox.framework框架
2.就一句代码:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
#import "ViewController.h"
//记得导入这个框架
#import
@interfaceViewController ()
{
SystemSoundID sound;
NSTimer*shakeTimer;
}
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//创建震动开始按钮
UIButton*startBtn_c=[[UIButtonalloc]initWithFrame:CGRectMake(180,200,100,44)];
startBtn_c.backgroundColor=[UIColorblueColor];
[startBtn_csetTitle:@"开始-C"forState:UIControlStateNormal];
[startBtn_caddTarget:selfaction:@selector(startButton_cClickedAction)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:startBtn_c];
//创建震动暂停按钮
UIButton*stopBtn_c=[[UIButtonalloc]initWithFrame:CGRectMake(40,200,100,44)];
stopBtn_c.backgroundColor=[UIColorredColor];
[stopBtn_csetTitle:@"暂停-C"forState:UIControlStateNormal];
[stopBtn_caddTarget:selfaction:@selector(stopButton_cClickedAction)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:stopBtn_c];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)stopButton_cClickedAction{
NSLog(@"stop button action");
//[audioPlayer stop];
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
[selfstopAlertSoundWithSoundID:sound];
}
-(void)stopAlertSoundWithSoundID:(SystemSoundID)sound {
AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);
}
-(void)startButton_cClickedAction{
NSLog(@"start button action");
//如果你想震动的提示播放音乐的话就在下面填入你的音乐文件
NSString*path = [[NSBundlemainBundle]pathForResource:@"marbach"ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURLfileURLWithPath:path], &sound);
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,NULL,NULL, soundCompleteCallback,NULL);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(sound);
}
voidsoundCompleteCallback(SystemSoundID sound,voidvoid* clientData) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动
AudioServicesPlaySystemSound(sound);
}
externOSStatus
AudioServicesAddSystemSoundCompletion( SystemSoundID inSystemSoundID,
CFRunLoopRef inRunLoop,
CFStringRef inRunLoopMode,
AudioServicesSystemSoundCompletionProc inCompletionRoutine,
void* inClientData)
__OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end