Recently to do a project, need to continue ringing and vibration, know that there is a private API can be used, but helpless to go online, for the sake of insurance, decisively give up, found on the Internet a method can be achieved as follows:
Register the following code in front of the code that plays the vibration:
1 audioservicesaddsystemsoundcompletion (ksystemsoundid_vibrate, NULL, NULL, Soundcompletecallback, NULL);
Where Soundcompletecallback is the playback system vibration or after the sound of the callback, you can continue to play the vibration on the inside to achieve continuous vibration function as follows:
void Soundcompletecallback (systemsoundid sound,voidvoid * clientdata) { Audioservicesplaysystemsound ( Ksystemsoundid_vibrate); Vibration Audioservicesplaysystemsound (sound); }
But the problem comes, how to stop, in fact, before I refer to others to do, is directly called the following code to stop:
Audioservicesremovesystemsoundcompletion (sound); Audioservicesdisposesystemsoundid (sound); Audioservicesremovesystemsoundcompletion (ksystemsoundid_vibrate); Audioservicesdisposesystemsoundid (ksystemsoundid_vibrate);
But I met a wonderful problem, is on the IOS10 test, other version I did not have a mobile phone test, the problem is that I found the call stop method, and did not immediately stop, the time of the stop is proportional to the time you play the vibration, the longer the vibration time, the call stopped, but also continue to vibrate for a period of time to stop, So I had a little headache. After the transformation, the realization of the necessary functions, with a timer, only for beginners reference, the big God do not spray, direct copy works are available:
1 @interface Viewcontroller ()2 {3 Systemsoundid Sound;4 }5 //Vibrating Timer6@property (nonatomic,strong) Nstimer *_vibrationtimer;7 @end8 9 @implementation ViewcontrollerTen @synthesize _vibrationtimer; One A- (void) Viewdidload { - [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. the - } - //start ringing and vibration --(ibaction) Startshakesound: (ID) sender{ + -NSString *path = [[NSBundle mainbundle] Pathforresource:@"2125"OfType:@"wav"]; +Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl Fileurlwithpath:path], &Sound ); A audioservicesaddsystemsoundcompletion (sound, NULL, NULL, soundcompletecallback, NULL); at Audioservicesplaysystemsound (ksystemsoundid_vibrate); - Audioservicesplaysystemsound (sound); - - - /** - Initialize the timer to vibrate once every second in - @param playksystemsound Vibration Method to @return + */ -_vibrationtimer = [Nstimer scheduledtimerwithtimeinterval:1target:self selector: @selector (playksystemsound) Userinfo:nil Repeats:yes]; the } * //Vibration $- (void) playksystemsound{Panax Notoginseng Audioservicesplaysystemsound (ksystemsoundid_vibrate); - } the //stop ringing and vibration +-(ibaction) Stopshakesound: (ID) sender{ A the [_vibrationtimer invalidate]; + audioservicesremovesystemsoundcompletion (sound); - Audioservicesdisposesystemsoundid (sound); $ $ } - //Ring callback method - voidSoundcompletecallback (Systemsoundid Sound,void*clientdata) { the Audioservicesplaysystemsound (sound); -}
Where the ringtone needs to be replaced, or the bell is removed demo:https://github.com/wtfubj/continuousvibrationdemo.git
iOS continues to vibrate if you want to stop.