Tom Fellas
Tom Fellas

Reputation: 11

look at this and tell me what im missing

I have this code, that on button press it plays a sound. it works on the simulator, but when I send it to my device for testing, I get this error; EXC_BAD_ACCESS (code=1, address=0x48). I have the latest version of xcode and I believe its in swift 5???. any help would be greatly appreciate it. also note I don't have a paid developer account.

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var musicEffect: AVAudioPlayer = AVAudioPlayer()

    @IBOutlet weak var muffin: UIImageView!
    @IBOutlet weak var muffin_text: UIImageView!
    @IBOutlet weak var muffin_button: UIButton!


    override func viewDidLoad() {

        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let musicFile = Bundle.main.path(forResource: "muffin", ofType: ".mp3")
        do {
            try musicEffect = try AVAudioPlayer(contentsOf: URL (fileURLWithPath: musicFile!))
        }    
        catch {
            print(error)
        }                  
    }        

    @IBAction func click(_ sender: Any) {

       musicEffect.play() 
    }
}

Upvotes: 0

Views: 65

Answers (2)

Caleb
Caleb

Reputation: 125007

it works on the simulator, but when I send it to my device for testing, I get this error

One big difference between iOS and macOS is that the filesystem on an iOS device is case-sensitive, whereas many (but not all) filesystems on macOS are case-insensitive. Whenever I see a question like "it works on the simulator but not on the device" I think this is probably related to reading a file.

So, check the capitalization of the file muffin.mp3 in your app. Is it actually Muffin.mp3 or muffin.MP3 or MUFFIN.mp3 or similar? If it's not exactly muffin.mp3, but instead has some capital letter, that would explain not finding the file. And your code doesn't check whether the value in musicEffect is valid before you try to play it, which explains why you might get an error.


Update: After looking at @Larme's comment, I think that incorrect initializer is more likely the problem if you're using iOS 13.1. See Assigning instance of AVAudioPlayer in iOS13 leads to BAD_ACCESS runtime error for example. AVAudioPlayer doesn't have a plain init() initializer, so you need to initialize it with some data, e.g.:

`var musicEffect: AVAudioPlayer = AVAudioPlayer(data: someAudioData)`

Upvotes: 0

Austin Monks
Austin Monks

Reputation: 11

I'm brand new to Xcode and Swift but I would remove the (.)mp3. I believe the dot is implied without having to write it.

Upvotes: 1

Related Questions