使用 swift 在 ios 上同时播放音频和振动

Play audio and vibration simultaneously on ios using swift

有什么方法可以播放音频文件并在音频开始时振动iphone而在音频停止播放时停止振动?

目前此代码正在运行,但它会播放音频,当音频停止时它会振动 phone,我希望两者(音频和振动)一起播放。

var alertSoundEffect: AVAudioPlayer?

let path = Bundle.main.path(forResource: "alert.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)

do {
    alertSoundEffect = try AVAudioPlayer(contentsOf: url)
    alertSoundEffect?.prepareToPlay()
    alertSoundEffect?.play()
} catch {
    return
}

let vib = SystemSoundID(1519)
AudioServicesPlayAlertSound(SystemSoundID(vib))
AudioServicesRemoveSystemSoundCompletion(vib)

你可以试试

 DispatchQueue.global().async 
 {
      var alertSoundEffect: AVAudioPlayer?

      let path = Bundle.main.path(forResource: "alert.mp3", ofType:nil)!
        let url = URL(fileURLWithPath: path)

        do {
            alertSoundEffect = try AVAudioPlayer(contentsOf: url)
            alertSoundEffect?.prepareToPlay()
            alertSoundEffect?.play()
        } catch {
            return
        }

 }