簡體   English   中英

使用Swift在iOS上同時播放音頻和振動

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

有什么方法可以播放音頻文件並在啟動音頻時振動iphone,並在音頻停止播放時停止振動?

目前,此代碼可以正常工作,但是會播放音頻,當音頻停止播放時,它會振動手機,我希望兩者(音頻和振動)一起播放。

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
        }

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM