2017-06-24 56 views
1

我每次按 a button時都會得到一個Thread1:EXC_BAD_ACCESS(code=1, address = 0X48)。我檢查了我的連接,我的mp3文件「歌曲」是正確的。我不確定有什麼問題。我的按鈕工作得很好,但它只是AVAudioPlayer (var Player),它不允許按鈕崩潰程序並返回錯誤。任何幫助將不勝感激(我是Swift的初學者)。Thread1:EXC_BAD_ACCESS(code = 1,address = 0X48)AVAudioPlayer(Swift)

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    var player:AVAudioPlayer = AVAudioPlayer() 

    @IBAction func play(_ sender: UIButton) { 

     player.play() 
    } 

    @IBAction func pause(_ sender: UIButton) { 
     player.pause() 
    } 

    @IBAction func replay(_ sender: UIButton) { 
     player.currentTime = 0 
    } 


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

      do{ 
       let audioPath = Bundle.main.path(forResource: "song", ofType: "mp3") 
       try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
      } 
      catch{ 
       //error 
      }           
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    }    
} 
+0

是的,我檢查了我的連接。我不確定player.play()有什麼問題 – Edward

回答

0

我寫相同的代碼項目,但沒有任何錯誤

我的代碼

var player:AVAudioPlayer = AVAudioPlayer() 
lazy var btn: UIButton = { 
    var btn = UIButton.init(frame: CGRect.init(x: 20, y: 20, width: 60, height: 60)) 
    btn.backgroundColor = .red 

    btn.addTarget(self, action: #selector(ViewController.play), for: .touchUpInside) 
    return btn 
}() 
override func viewDidLoad() { 
    super.viewDidLoad() 

    self.view.addSubview(btn) 
    do{ 
     let audioPath = Bundle.main.path(forResource: "song", ofType: "mp3") 
     try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
    } 
    catch{ 
     //error 
    } 
} 
func play() { 
    player.play() 
} 

請您這個問題

1.Whether玩家在主運行線程?

2.song.mp3是存在的,它可以玩

3.故事板是否衝突

相關問題