ios - AVAudioPlayer 为路径 Swift 3 xcode 8.0 提供 nill

标签 ios swift xcode avaudioplayer

我的音频播放器有问题。当我启动我的 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 文件未列出,则将它们添加到该组中。

清理您的项目并再次运行。

关于ios - AVAudioPlayer 为路径 Swift 3 xcode 8.0 提供 nill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177182/

相关文章:

ios - 如何在 iOS 上通过 UITextFields 进行枚举

ios - 无法在 iOS 中获取所有 Facebook 好友

ios - 为 iOS 配置 Firebase/Messaging 的问题

ios - 用于更新 View 的 Swift 主循环

ios - 编译到模拟器时如何在 Xcode 中排除框架

ios - 在文本标签中显示数组

ios - 总是调用 Swift LocationManager didChangeAuthorizationStatus

arrays - FOR IN 循环出错

objective-c - 如何检测 OSX 上正在运行什么应用程序

objective-c - 在 iOS 的 icarousel 中使用 AsyncImageView 的问题