简体   繁体   English

使用Swift在iOS上同时播放音频和振动

[英]Play audio and vibration simultaneously on ios using swift

Is there any way to play audio file and vibrate iphone when audio is started and stop vibrating when audio stops playing? 有什么方法可以播放音频文件并在启动音频时振动iphone,并在音频停止播放时停止振动?

currently this code is working but it plays audio and when audio stops then it vibrates the phone, I want both (audio and vibration) play together. 目前,此代码可以正常工作,但是会播放音频,当音频停止播放时,它会振动手机,我希望两者(音频和振动)一起播放。

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)

You can try 你可以试试

 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
        }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM