[英]AVAudioPlayer does not play audio file - swift
我知道它似乎很簡單,但是以一種奇怪的方式,AVAudioPlayer無法播放音頻文件。 我有這個代碼。 它沒有通過捕捉,仍然沒有聲音播放。 我已經檢查了物理設備,模擬器以及音量。 但是我還是聽不到聲音。
//in ViewController as a property
let soundEffect: AVAudioPlayer!
//in viewDidLoad
let path = Bundle.main.path(forResource: "success.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
soundEffect = try AVAudioPlayer(contentsOf: url)
soundEffect.prepareToPlay()
soundEffect.play()
} catch let error as NSError {
print(error.description)
}
我也嘗試過let path = Bundle.main.path(forResource: "success", ofType:"mp3")!
但這沒什么區別。
嘗試將AVAudioPlayer設置為實例變量。 否則,soundEffect將在do循環的末尾釋放。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.