This is a question related to Are there APIs for custom vibrations in iOS?. I am able to create custom vibration patterns, but have no control over the intensity.
This is copied over from Kevin Cao's answer that enables custom vibration patterns:
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];
[arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms
[arr addObject:[NSNumber numberWithInt:2000]];
[arr addObject:[NSNumber numberWithBool:NO]]; //stop for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];
[arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];
[arr addObject:[NSNumber numberWithBool:NO]]; //stop for 500ms
[arr addObject:[NSNumber numberWithInt:500]];
[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithInt:1] forKey:@"Intensity"];
AudioServicesPlaySystemSoundWithVibration(4095,nil,dict);
The line of code that adds the key @"Intensity"
with an int
value doesn't do the trick and I don't know how to look inside the AudioServicesPlaySystemSoundWithVibration
method to figure it out. What do I have to pass to it so that it actually changes the intensity?
Right now, it doesn't matter if I pass 1, 1000, 0.4 or 0.0001, it's always the same intensity (on an iPhone 4 with iOS7). Can anyone recreate this?
I would like to be able not only to create vibration patterns, but a smooth vibration envelope. How to?
(As this is a research project for instrument design, I am not (yet) concerned with the App store restrictions.)
Change the
numberWithInt
call intonumberWithFloat
, and change the intensity so it's between 0 and 1. I thought it was weird when they used anint
rather than afloat
.Edit: Here's a copy/paste that should work for your code to invoke custom vibration:
For
startStopTimes
, it should alternate between times started and times stopped. Passing in this array:Will do what the example code did. In this case, it will start for 2000 ms, stop for 1000 ms, start for 1000 ms, and stop for 500 ms.
stop
is called to stop the sound. The way I have it set up, it stops sounds after the total amount of time sent in.You may have noticed I've been using array/number literals rather than using
[NSArray arrayWithObjects: ... , nil];
or[NSNumber numberWith...];
. This makes your code a lot shorter. Also, I marked the beginning with a#pragma mark
. Use that to organize it better. Hope it helps!