简体   繁体   中英

NSURL Array Error in Swift?

So I'm trying to create a variable that I can use to pick a random sound from an NSURL array I created. This is the array, which is the line that causes the error:

var levelOneSounds = 
    [
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Barn", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Chicken", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Birds", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Camera", ofType: "mp3")!), NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Cat", ofType: "mp3")!), 
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Crickets", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Glass", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Siren", ofType: "mp3")!), NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Slap", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Snoring", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Toilet Flushing", ofType: "mp3")!),
        NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Wolves Howling", ofType: "mp3")!)
    ]

Obviously, all of these are mp3 files that I stored in the Supporting Files folder. I also created the audio player here:

var levelOneAudioPlayer = AVAudioPlayer()

Here is my viewDidLoad() method where I create the random sound variable and play that sound:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    var randomSound = levelOneSounds[Int(arc4random_uniform(UInt32(levelOneSounds.count)))]

    levelOneAudioPlayer = AVAudioPlayer(contentsOfURL: randomSound, error: nil)
    levelOneAudioPlayer.play()
}

However whenever I run this and go into this view controller, I get the following error at the array:

Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0 x 0)

What can I do to fix this so the random sound is played? I'm new to programming, so I apologize if I made an obvious mistake. Thanks!

Here is a link to the photo of the resource folder: http://i.stack.imgur.com/bj3yG.png

The error occurs because your sounds are located in a subfolder of the resources folder.

The basic function pathForResource:ofType: does not consider subfolders.

From the documentation:

The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. If a matching resource file is not found, it then looks in the top level of an available language-specific .lproj folder. (The search order for the language-specific folders corresponds to the user's preferences.) It does not recurse through other subfolders at any of these locations .

You have to use this function

NSBundle.mainBundle().pathForResource("Barn", ofType: "mp3", inDirectory:"Level 1")

or this much more effective function to get all URLs with one line

let levelOneSounds = NSBundle.mainBundle().URLsForResourcesWithExtension("mp3", subdirectory:"Level 1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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