User Notification: Custom Vibration pattern

戏子无情 提交于 2019-12-10 18:21:23

问题


Is it possible to create custom vibration pattern for the user notification alert? example, we can choose to have different audio for the user notification. Is it possible to have custom vibration pattern as well?

I mean do this programmatically on iOS using swift.


回答1:


For create custom vibrations in iOS. Use AudioServicesPlaySystemSoundWithVibration and AudioServicesStopSystemSound.

Heart Beat Vibration Example

NSMutableDictionary* pulsePatternsDict = [@{} mutableCopy];
        NSMutableArray* pulsePatternsArray = [@[] mutableCopy];

        // beat for 100 times
        for (NSInteger i=0; i<100; i++){
            [pulsePatternsArray addObject:@(YES)]; // vibrate for 100ms
            [pulsePatternsArray addObject:@(100)];

            [pulsePatternsArray addObject:@(NO)];  //stop for 1200ms * 0.3
            [pulsePatternsArray addObject:@(1200*0.3)];

            [pulsePatternsArray addObject:@(YES)];  //vibrate for 100ms
            [pulsePatternsArray addObject:@(100)];

            [pulsePatternsArray addObject:@(NO)];    //stop for 1200ms * 0.5
            [pulsePatternsArray addObject:@(1200*0.5)];
        }

        [pulsePatternsDict setObject:pulsePatternsArray forKey:@"VibePattern"];
        [pulsePatternsDict setObject:[NSNumber numberWithInt:1.0] forKey:@"Intensity"];

        AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, pulsePatternsDict);

But only call this (AudioServicesPlaySystemSoundWithVibration ) will make a vibration never stop. So I have to found some function to stop it.

The answer is AudioServicesStopSystemSound. It's also a private function in AudioToolbox framework.

The declaration of the function is like

void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)

NOTE: AudioServicesPlaySystemSoundWithVibration is only available on iOS6

There is currently no public available API in iOS 9 and iOS 9.1.

And In iOS 10 there's a new API called UIFeedbackGenerator. See this post for more details. It only works on the iPhone 7 and iPhone 7 Plus for now.

Disclaimer: There is a way to interact with Taptic Engine (iOS 9) directly, but there is a private method. You should not use it in App Store applications.




回答2:


You can try this. In the below code you can get different types of vibrations with audio and without audio. make sure that you have imported these (import AudioToolbox, import AVFoundation)

if(selectedRow == 0){
          AudioServicesPlaySystemSound(SystemSoundID(1304))
        }else if(selectedRow == 1){
             AudioServicesPlaySystemSound(SystemSoundID(1329))
        }else if(selectedRow == 2){
            AudioServicesPlaySystemSound(SystemSoundID(1301))
        }else if(selectedRow == 3){
           AudioServicesPlaySystemSound(SystemSoundID(1027))
        }else if(selectedRow == 4){
           AudioServicesPlaySystemSound(SystemSoundID(1028))
        }else if(selectedRow == 5){
            let alert = SystemSoundID(1011)
            AudioServicesPlaySystemSoundWithCompletion(alert, nil)
        }else if(selectedRow == 6){
           AudioServicesPlaySystemSound(SystemSoundID(1333))
        }else if(selectedRow == 7){
           AudioServicesPlaySystemSound(SystemSoundID(4095))
        }


来源:https://stackoverflow.com/questions/45101474/user-notification-custom-vibration-pattern

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