在swift 2中无法播放“m4a”。

时间:2016-06-13 06:20:18

标签: ios swift2 avfoundation

我是swift的新手。我试图在swift 2中使用avfoundation运行音频文件。这些代码运行文件格式“mp3”但xcode崩溃为“m4a”。我的错是什么?我使用的是xcode 7.3.1。

类ViewController:UIViewController {

 var myAudioPlayer = AVAudioPlayer()
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let myFilePathString = NSBundle.mainBundle().pathForResource("Par30", ofType:"m4a")

    if let myFilePathString = myFilePathString
    {
        let myFilePathURL = NSURL(fileURLWithPath: myFilePathString)


        do{

            try myAudioPlayer = AVAudioPlayer(contentsOfURL: myFilePathURL)

            //myAudioPlayer.play()

        }catch
        {
            print("error")
        }
    }
}


@IBAction func Play(sender: AnyObject) {

    myAudioPlayer.play()

}


@IBAction func Stop(sender: AnyObject) {

    myAudioPlayer.stop()
    myAudioPlayer.currentTime = 0
  }

}

0 个答案:

没有答案