随机音频文件我不断收到以下错误,当我使用“摇晃手势”在iPhone模拟器:播放使用雨燕
Fatal error: unexpectedly found nil while unwrapping an Optional value
这里是我的相关代码:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var soundFiles = ["kidLaughing", "pewpew", "pingas", "runningfeet"]
var player: AVAudioPlayer = AVAudioPlayer()
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if event.subtype == .MotionShake {
var randomSoundFile = Int(arc4random_uniform(UInt32(soundFiles.count)))
var fileLocation = NSString(string:NSBundle.mainBundle().pathForResource("sounds/" + soundFiles[randomSoundFile], ofType: "mp3")!)
var error: NSError? = nil
player = AVAudioPlayer(contentsOfURL: NSURL(string: fileLocation), error: &error)
player.play()
}
}
}
我有一个名为sounds
的文件夹,其中有4个mp3
文件。该错误是发生在这行代码:
var fileLocation = NSString(string:NSBundle.mainBundle().pathForResource("sounds/" + soundFiles[randomSoundFile], ofType: "mp3")!)
我已经试过所有我能想到的来得到这个工作,但没有我曾尝试工作过。任何帮助表示赞赏!
完美!谢谢! – Jeff 2016-11-13 21:35:41
你永远欢迎 – Rex 2017-02-07 08:20:39