我的应用程序中有几个地方有音频,其中大部分是直接的并且工作正常,例如
func ayuh() {
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ayuhSound2", ofType: "mp3")!))
audioPlayer!.play()
} catch {
print("Error")
}
}
现在,我正尝试在我的 plist 播放中添加与特定问题相关的音频文件。我试过这个:
func nextQuestion() {
let currentQuestion = mcArray![questionIdx]
answers = currentQuestion["Answers"] as! [String]
correctAnswer = currentQuestion["CorrectAnswer"] as? String
question = currentQuestion["Question"] as? String
cardButton.enabled = false
titlesForButtons()
self.bonusCoin.hidden = true
//this code above works to bring up the question and various answers
audio = currentQuestion["Audio"] as? String
do {
mcaudioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(audio, ofType: "mp3")!))
mcaudioPlayer!.play()
} catch {
print("Error")
}
mcaudioPlayer!.play()
} catch {
print("Error")
}
但这只会针对每个问题一遍又一遍地播放列表中的第一个音频文件。 here are the audio files so far
I want the before_lepage.mp3 to play when this question appears from the array in my trivia app
有什么线索可以播放 plist 中关联的音频文件吗?
最佳答案
我成功了(在 friend 的帮助下 ;))
func nextQuestion() {
let currentQuestion = mcArray![questionIdx]
answers = currentQuestion["Answers"] as! [String]
correctAnswer = currentQuestion["CorrectAnswer"] as? String
question = currentQuestion["Question"] as? String
cardButton.enabled = false
titlesForButtons()
self.bonusCoin.hidden = true
audio = currentQuestion["Audio"] as? String
createPlayerItem(audio!, ofType: "mp3")
queuePlayer.play()
//queuePlayer.removeAllItems()
}
func createPlayerItem(item: String, ofType type: String) {
let path = NSBundle.mainBundle().pathForResource(item, ofType: type)
let url = NSURL.fileURLWithPath(path!)
let item = AVPlayerItem(URL: url)
queuePlayer.insertItem(item, afterItem: nil)
}
关于swift - 播放与 plist 中的项目关联的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37046467/