Swift 播放与plist中的项目关联的音频文件
我的应用程序中有好几个地方有音频,大部分都是直接的,工作正常,比如Swift 播放与plist中的项目关联的音频文件,swift,audio,Swift,Audio,我的应用程序中有好几个地方有音频,大部分都是直接的,工作正常,比如 func ayuh() { do { audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ayuhSound2", ofType: "mp3")!)) audioPlayer!.play()
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")
}
但这只会为每个问题反复播放列表中的第一个音频文件。
有什么线索可以让它播放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)
}