Intensity of custom iPhone vibration

只谈情不闲聊 提交于 2019-12-04 13:27:30

问题


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.)


回答1:


Change the numberWithInt call into numberWithFloat, and change the intensity so it's between 0 and 1. I thought it was weird when they used an int rather than a float.


Edit: Here's a copy/paste that should work for your code to invoke custom vibration:

#pragma mark - Custom vibration methods
-(void)invokeCustomVibrationWithStartStopTimes:(NSArray*)startStopTimes andIntensity:(float)intensity {
    BOOL startOrStop = YES;
    NSMutableArray* arr = [@[] mutableCopy];
    double time = 0;
    for (NSNumber *x in stopStartTimes) {
        [arr addObject:x]
        startOrStop = !startOrStop;
        [arr addObject:@(startOrStop)];
        time = [x doubleValue] / 1000.0;
    }
    AudioServicesPlaySystemSoundWithVibration(4095,nil,{@"VibePattern":arr,@"Intensity":@(intensity)})
    [self performSelector:@selector(stop) withObject:nil afterDelay:time];
}

-(void)stop {
    AudioServicesStopSystemSound(4095); // stop buzzing the phone
}

For startStopTimes, it should alternate between times started and times stopped. Passing in this array:

@[@(2000), @(1000), @(1000), @(500)]

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!



来源:https://stackoverflow.com/questions/19822295/intensity-of-custom-iphone-vibration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!