[英]Sound plays on emulator but not on iPhone
我正在做一個教程,並且測試了這段代碼,當我在模擬器上運行時,它可以工作,但是在iPhone上進行測試時,它不能。 我調試了,他沒弄錯,只是不播放聲音。
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer = AVAudioPlayer()
@IBAction func notePressed(_ sender: UIButton) {
playSound(tag: sender.tag)
}
func playSound(tag: Int) {
let soundURL = Bundle.main.url(forResource: "note\(tag)", withExtension: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
} catch {
print(error)
}
audioPlayer.numberOfLoops = -1
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
您必須在播放音頻之前設置AVAudioSession.sharedInstance().setCategory(.playback)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.