Mam audio w kilku miejscach w mojej aplikacji, a większość z nich jest prosta i działa dobrze, jak
func ayuh() {
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ayuhSound2", ofType: "mp3")!))
audioPlayer!.play()
} catch {
print("Error")
}
}
Teraz próbuję mieć pliki audio dołączone do konkretnych pytań w mojej grze plist. Próbowałem tego:
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")
}
ale to odtwarza tylko pierwszy plik audio na liście w każdym pytaniu. oto pliki audio do tej pory
Jakieś wskazówki, w jaki sposób mogę odtworzyć plik audio skojarzony z plist?
Odpowiedzi:
0 dla odpowiedzi № 1Mam to działa (z pomocą przyjaciół;))
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)
}