AVAudioPlayer 给路径 nill Swift 3 xcode 8.0
AVAudioPlayer gives nill for the path Swift 3 xcode 8.0
我的音频播放器有问题。当我启动我的 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()
}
}
音频文件位于根目录。
有人看到问题了吗?
亲切的问候,
凯文.
选中yourProject -> Target's Build
,在Copy Bundle Resources 阶段组下,,如果您的m4a 文件未列出,则将它们添加到该组中。
清理您的项目并再次运行。
我的音频播放器有问题。当我启动我的 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()
}
}
音频文件位于根目录。
有人看到问题了吗?
亲切的问候, 凯文.
选中yourProject -> Target's Build
,在Copy Bundle Resources 阶段组下,,如果您的m4a 文件未列出,则将它们添加到该组中。
清理您的项目并再次运行。