0
我試圖播放聲音與AVAudioPlayer,但它不能播放。我試了一個設備(iPhone 6s)和模擬器。我沒有收到任何錯誤,並將其記錄下來。當我在audioPlayer.play()之前設置一個斷點並跳過時,它會播放幾秒鐘,然後停止。我的代碼:斯威夫特2 - AVAudioPlayer不播放音頻
let soundURL = NSBundle.mainBundle().URLForResource("test", withExtension: "mp3")
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
if let soundURL = soundURL {
let audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL)
audioPlayer.volume = 1
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
print(audioPlayer.duration)
audioPlayer.play()
print("PLAYED")
}
}catch {
print("Error getting the audio file")
}
代碼的其餘部分顯示的代碼在哪裏?這一切都在viewDidLoad? – MikeG