私のコードでこの問題が発生しています:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
このメッセージは、私のコードの次の行に表示されます。
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation!))
どんな助けでも大歓迎です。前もって感謝します!
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if event?.subtype == UIEvent.EventSubtype.motionShake {
let fileLocation = Bundle.main.path(forResource: "sound1", ofType: "mp3")
do {
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation!))
player.play()
} catch {
// process error
}
}
}
}