I get the optional unwrapping error, but I can't see what is producing it. I checked the location of the mp3 file several times, I tracked down every line in the debug mode and it seems like it is getting a right file path in laughSound
Here is 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.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Laughing(sender: UIButton) {
// Set the sound file name & extension
var laughSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Laughing", ofType: "mp3")!)
// Play the sound
audioPlayer = AVAudioPlayer(contentsOfURL: laughSound, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
It gives me an error in this line
audioPlayer.prepareToPlay()
What should be the exact location of the audio file? I store it in the Supporting files and drag it to Xcode directly. Or what else can cause this problem? I've exhausted my search results.