Swift-audio execution crash: "EXC_BAD_INSTRUCTION"

I am trying to put audio in my application and I entered the following code and put my mp3 file in my file folder and it crashed: "EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0x0)"

I could really use some help on what I am doing wrong / where I need to put the audio file. My code:

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var audioPlayer = AVAudioPlayer()

        override func viewDidLoad() {
        super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    ScrollView.scrollEnabled = true
    ScrollView.contentSize = CGSize(width:473, height: 112)
    changer = 0
    tapView.hidden = true
    yoohooView.hidden = true
    var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Untitled", ofType: "mp3")!) //Crashes here
        println(alertSound)

        // Removed deprecated use of AVAudioSessionDelegate protocol
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)

        var error:NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
        audioPlayer.prepareToPlay()
        audioPlayer.play()

}

      

+3


source to share


1 answer


class ViewController: UIViewController {

var audioPlayer = AVAudioPlayer()

@IBOutlet weak var svScrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    svScrollView.scrollEnabled = true
    svScrollView.contentSize = CGSize(width:473, height: 112)
    var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myMusic", ofType: "mp3")!)
    println(alertSound)
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)
    var error:NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
    audioPlayer.prepareToPlay()
    audioPlayer.play()
} 
}

      



I tried the code ... and it works fine for me ... Please try this ...

+1


source







All Articles