Swift 中的 NSURL 数组错误?
NSURL Array Error in Swift?
所以我正在尝试创建一个变量,我可以使用它从我创建的 NSURL 数组中选择随机声音。这是数组,也就是导致错误的行:
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")!)
]
显然,所有这些都是我存储在Supporting Files 文件夹中的mp3 文件。我还在这里创建了音频播放器:
var levelOneAudioPlayer = AVAudioPlayer()
这是我的 viewDidLoad()
方法,我在其中创建随机声音变量并播放该声音:
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()
}
然而,每当我 运行 这个并进入这个视图控制器时,我在数组中得到以下错误:
Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0 x 0)
如何解决此问题以便播放随机声音?我是编程新手,所以如果我犯了一个明显的错误,我深表歉意。谢谢!
这里是资源文件夹的照片link:
http://i.stack.imgur.com/bj3yG.png
发生错误是因为您的声音位于资源文件夹的子文件夹中。
基本功能pathForResource:ofType:
不考虑子文件夹。
来自文档:
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.
你必须使用这个功能
NSBundle.mainBundle().pathForResource("Barn", ofType: "mp3", inDirectory:"Level 1")
或使用一行获取所有 URL 的更有效的函数
let levelOneSounds = NSBundle.mainBundle().URLsForResourcesWithExtension("mp3", subdirectory:"Level 1")
所以我正在尝试创建一个变量,我可以使用它从我创建的 NSURL 数组中选择随机声音。这是数组,也就是导致错误的行:
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")!)
]
显然,所有这些都是我存储在Supporting Files 文件夹中的mp3 文件。我还在这里创建了音频播放器:
var levelOneAudioPlayer = AVAudioPlayer()
这是我的 viewDidLoad()
方法,我在其中创建随机声音变量并播放该声音:
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()
}
然而,每当我 运行 这个并进入这个视图控制器时,我在数组中得到以下错误:
Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0 x 0)
如何解决此问题以便播放随机声音?我是编程新手,所以如果我犯了一个明显的错误,我深表歉意。谢谢!
这里是资源文件夹的照片link: http://i.stack.imgur.com/bj3yG.png
发生错误是因为您的声音位于资源文件夹的子文件夹中。
基本功能pathForResource:ofType:
不考虑子文件夹。
来自文档:
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.
你必须使用这个功能
NSBundle.mainBundle().pathForResource("Barn", ofType: "mp3", inDirectory:"Level 1")
或使用一行获取所有 URL 的更有效的函数
let levelOneSounds = NSBundle.mainBundle().URLsForResourcesWithExtension("mp3", subdirectory:"Level 1")