So I'm trying to play a random audio track from an array when a certain view loads. Everything's fine, but when I go to load that view, I get an error. Here is the class code of the View Controller:
class SoundGameViewController: UIViewController {
var levelOneSounds : [NSURL]!
var levelOneAudioPlayer: AVAudioPlayer! = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var error : NSError?
levelOneSounds = NSBundle.mainBundle().URLsForResourcesWithExtension("mp3", subdirectory: "Level 1") as! [NSURL]
var randomSound = levelOneSounds[Int(arc4random_uniform(UInt32(levelOneSounds.count)))]
levelOneAudioPlayer = AVAudioPlayer(contentsOfURL: randomSound, error: &error)
if error != nil { println(error!) }
levelOneAudioPlayer.play()
}
The error shows up on the last line, levelOneAudioPlayer.play()
. This is the error:
Thread 1: EXC_BAD_INSTRUCTION . . . (code = exc_1386_INVOP, subcode = 0 x 0
However, with one of the sounds, it actually ends up working and plays the sound when the view loads. But with all the other sounds, the view doesn't load at all and I get the error. Sometimes, the view does load but I get no sound and no error. Here is an image of the folders I hold all of the sounds in: https://i.stack.imgur.com/JBdGU.png
I have tried numerous times asking questions on Stack Overflow and research about it, but I just can't seem to get it. I have also looked for many tutorials on debugging, but I guess I'm not experienced enough to really understand what I need to do in this situation. I'm 13, and I don't have Apple Developer for much longer, and this is becoming really bothersome. I'll go as far as sending someone the project if they can fix this error.
I'm new to programming, so I'm sorry if I made an obvious mistake. Thanks!