how to run vibrate continuously in iphone?

后端 未结 4 498
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 10:46

In my application I\'m using following coding pattern to vibrate my iPhone device

Include: AudioToolbox framework

Header File:

#         


        
4条回答
  •  长发绾君心
    2020-12-28 11:39

    Thankfully, it's not possible to change the duration of the vibration. The only way to trigger the vibration is to play the kSystemSoundID_Vibrate as you have. If you really want to though, what you can do is to repeat the vibration indefinitely, resulting in a pulsing vibration effect instead of a long continuous one. To do this, you need to register a callback function that will get called when the vibration sound that you play is complete:

     AudioServicesAddSystemSoundCompletion (
            kSystemSoundID_Vibrate,
            NULL,
            NULL,
            MyAudioServicesSystemSoundCompletionProc,
            NULL
        );
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    

    Then you define your callback function to replay the vibrate sound again:

    #pragma mark AudioService callback function prototypes
    void MyAudioServicesSystemSoundCompletionProc (
       SystemSoundID  ssID,
       void           *clientData
    );
    
    #pragma mark AudioService callback function implementation
    
    // Callback that gets called after we finish buzzing, so we 
    // can buzz a second time.
    void MyAudioServicesSystemSoundCompletionProc (
       SystemSoundID  ssID,
       void           *clientData
    ) {
      if (iShouldKeepBuzzing) { // Your logic here...
          AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
      } else {
          //Unregister, so we don't get called again...
          AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
      }  
    }
    

提交回复
热议问题