简体   繁体   English

Thread1:EXC_BAD_ACCESS(代码= 1,地址= 0X48)AVAudioPlayer(Swift)

[英]Thread1:EXC_BAD_ACCESS(code=1, address = 0X48) AVAudioPlayer (Swift)

I get a Thread1:EXC_BAD_ACCESS(code=1, address = 0X48) every time I press a button . 每次按下button Thread1:EXC_BAD_ACCESS(code=1, address = 0X48)我都会得到一个Thread1:EXC_BAD_ACCESS(code=1, address = 0X48) I checked my connections and my mp3 file "song" is correct. 我检查了我的连接,我的mp3文件“歌曲”是正确的。 I'm not sure what's wrong. 我不确定是什么问题。 My buttons work perfectly fine but it's just the AVAudioPlayer (var Player) that is not allowing the buttons to crash the program and return the error. 我的按钮工作得很好,但它只是AVAudioPlayer (var Player)不允许按钮崩溃程序并返回错误。 Any help would be much appreciated (I'm a beginner at Swift). 任何帮助将不胜感激(我是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.
    }            
}

I write same code project ,but not have any wrong 我写了相同的代码项目,但没有任何错误

My code 我的代码

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()
}

please check you this problem 请检查一下这个问题

1.Whether the player is running on the main thread? 1.玩家是否在主线程上运行?

2.song.mp3 is exist and it can playing 2.song.mp3存在且可以播放

3.Whether the storyboard is in conflict 3.故事板是否存在冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM