【问题标题】:AVAudioPath not working - SwiftAVAudioPath 不工作 - 斯威夫特
【发布时间】:2016-02-16 20:51:25
【问题描述】:

我正在尝试根据用户选择的乐器播放歌曲。这是我的代码:

@IBAction func play(sender: AnyObject) {

    if isPlaying == false {
        player.play()
        isPlaying = true
    }

}
@IBAction func stop(sender: AnyObject) {

    if isPlaying == true {
        player.stop()
        isPlaying = false
    }

}
@IBAction func pause(sender: AnyObject) {

    if isPlaying == true {
        isPlaying = false
        player.pause()
    }

}



var player = AVAudioPlayer()
var audioPath = NSBundle.mainBundle().pathForResource("Iditarod Bass", ofType: "m4a")

var isPlaying = Bool()

@IBOutlet var instrumentSelect: UISegmentedControl!


@IBAction func didChangeInstrument(sender: AnyObject) {

    if isPlaying == false {

        if instrumentSelect.selectedSegmentIndex == 0 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Bass", ofType: "m4a")

            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
            } catch {


            }

        } else if instrumentSelect.selectedSegmentIndex == 1 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Cello", ofType: "m4a")

                do {
                    try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
                } catch {


                }


        } else if instrumentSelect.selectedSegmentIndex == 2 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Viola", ofType: "m4a")
            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
            } catch {


            }

        } else if instrumentSelect.selectedSegmentIndex == 3 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Violin 1", ofType: "m4a")

            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
            } catch {


            }

        } else if instrumentSelect.selectedSegmentIndex == 4 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Violin 2", ofType: "m4a")

            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))

            } catch {


            }


        }
    }

}

除了instrumentSelect.selectedSegmentIndex == 1 之外,它们都可以正常工作。我不知道为什么。我收到一个EXC_BAD_INSTRUCTION,上面写着fatal error: unexpectedly found nil while unwrapping an Optional value。该文件工作正常,我已经多次清理该项目。我已经正确输入了所有内容。为什么那个不工作?我对此感到非常困惑,感谢您的帮助。

【问题讨论】:

  • 您说“文件工作正常”,但显然不是。我的猜测是它可能在您的 project 中,但您忘记将其添加到您的 app target 中,因此它没有内置到应用程序中。
  • 谢谢@matt!!!有效。通过正常工作,我的意思是当我点击侧面的文件时,它会运行。但它没有内置到应用程序中。感谢您的帮助

标签: xcode swift avaudioplayer exc-bad-instruction


【解决方案1】:

文件未添加到应用程序目标。它仅添加到项目中。感谢@matt 帮助我。

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2017-07-22
    • 2017-03-24
    相关资源
    最近更新 更多