Vibration - How kSystemSoundID_Vibrate actually works?

Hello I starting with iOS coding and actually I got what I wanted. But I didn't understand how! Please, look my code. In the btn2Action, I wanted the iphone 2 times and in the btn3Action 3 times, but to achieve this goal, in my "do while loop" I put 400 and 1000 as the limit, but as far the knowledge I have, the AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) should play each loop's iteration right?


https://www.dropbox.com/s/45gn4xqlwd9opwx/Screenshot%202015-08-01%2019.58.50.png?dl=0

I believe this is not a problem of Swift.


It seems AudioServicesPlaySystemSound returns immediately and ignores other calls while playing.

Searching with "ios repeat vibration", I could easily find a Stack Overflow article titled "how to run vibrate continuously in iphone?".

I translated the code into Swift.

In toplevel:

@objc protocol AudioServicesPlaySystemSoundDelegate {
    func audioServicesPlaySystemSoundCompleted(soundId: SystemSoundID)
}
func MyAudioServicesSystemSoundCompletionHandler(soundId: SystemSoundID, inClientData: UnsafeMutablePointer<Void>) {
    let delegate = unsafeBitCast(inClientData, AudioServicesPlaySystemSoundDelegate.self)
    delegate.audioServicesPlaySystemSoundCompleted(soundId)
}

In your ViewController:

class SysSoundViewController: UIViewController, AudioServicesPlaySystemSoundDelegate  {
    //...
    private var repeatCount: Int = 3
    @IBAction func repeatVibration(_: AnyObject) {
        let proc: AudioServicesSystemSoundCompletionProc = MyAudioServicesSystemSoundCompletionHandler
        AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, nil, nil, proc, UnsafeMutablePointer(unsafeAddressOf(self)))
        self.repeatCount = 3
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
    func audioServicesPlaySystemSoundCompleted(soundId: SystemSoundID) {
        if( --repeatCount > 0 ) {
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        }
    }
    //...
}
Vibration - How kSystemSoundID_Vibrate actually works?
 
 
Q