我的应用程序中有几个地方有音频,大部分都是直接的,可以很好地工作,比如
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")
}
但这只是针对每个问题反复播放列表中的第一个音频文件。这是到目前为止的音频文件
当这个问题从我的琐事应用中的数组中出现时,我想播放before_leage.mp3
有什么线索可以让它播放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
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)
}