我的音频播放器出了问题。当我启动我的viewcontroller时,它会在url上给出nill,我不知道这是怎么回事。我试着调试并查找问题,但我不知道发生了什么。
以下是代码:
// VARS AUDIO
var sfxFive: AVAudioPlayer!
var sfxFour: AVAudioPlayer!
var sfxThree: AVAudioPlayer!
var sfxtwo: AVAudioPlayer!
var sfxOne: AVAudioPlayer!
var sfxGo: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
do {
let resourcePathFive = Bundle.main.path(forResource: "five", ofType: "m4a")!
let urlFive = URL(fileURLWithPath: resourcePathFive)
try sfxFive = AVAudioPlayer(contentsOf: urlFive)
let resourcePathFour = Bundle.main.path(forResource: "four", ofType: "m4a")!
let urlFour = URL(fileURLWithPath: resourcePathFour)
try sfxFour = AVAudioPlayer(contentsOf: urlFour)
let resourcePathThree = Bundle.main.path(forResource: "three", ofType: "m4a")!
let urlThree = URL(fileURLWithPath: resourcePathThree)
try sfxThree = AVAudioPlayer(contentsOf: urlThree)
let resourcePathTwo = Bundle.main.path(forResource: "two", ofType: "m4a")!
let urlTwo = URL(fileURLWithPath: resourcePathTwo)
try sfxtwo = AVAudioPlayer(contentsOf: urlTwo)
let resourcePathOne = Bundle.main.path(forResource: "one", ofType: "m4a")!
let urlOne = URL(fileURLWithPath: resourcePathOne)
try sfxOne = AVAudioPlayer(contentsOf: urlOne)
let resourcePathGo = Bundle.main.path(forResource: "go", ofType: "m4a")!
let urlGo = URL(fileURLWithPath: resourcePathGo)
try sfxGo = AVAudioPlayer(contentsOf: urlGo )
sfxFive.prepareToPlay()
sfxFour.prepareToPlay()
sfxThree.prepareToPlay()
sfxtwo.prepareToPlay()
sfxOne.prepareToPlay()
sfxGo.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
}
func timerRunning() {
if timeElapsed == 5 {
sfxFive.play()
}else if timeElapsed == 4 {
sfxFour.play()
}else if timeElapsed == 3 {
sfxThree.play()
}else if timeElapsed == 2 {
sfxtwo.play()
}else if timeElapsed == 1 {
sfxOne.play()
}else if timeElapsed == 0 {
sfxGo.play()
}
}
音频文件位于根目录中。
有人看到了这个问题吗?
亲切的问候, 凯文。
答案 0 :(得分:1)
检查yourProject -> Target's Build
,在复制捆绑资源阶段组下的中,如果未列出您的m4a文件,则在该组中添加它们。
清理项目并再次运行。