Reputation: 5751
Here's part of the code:
class AudioRecorderController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder: AVAudioRecorder?
func audioRecordingPath() -> NSURL {
let fileManager = NSFileManager()
let documentsFolderUrl = fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false, error: nil)
return documentsFolderUrl!.URLByAppendingPathComponent("Recording.m4a")
}
func audioRecordingSettings() -> NSDictionary {...}
func startRecordingAudio() {
var error: NSError?
let audioRecordingURL = self.audioRecordingPath()
audioRecorder = AVAudioRecorder(URL: audioRecordingURL, settings: audioRecordingSettings() as [NSObject: AnyObject], error: &error)
if let recorder = audioRecorder {
recorder.delegate = self
if recorder.prepareToRecord() && recorder.record() {
println("Capture Succeed!")
let delayInSeconds = 5.0
let delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(delayInNanoSeconds, dispatch_get_main_queue(), {
[weak self] in
self!.audioRecorder!.stop()
//It breaks here.
})
}
}
}
It printed "Capture succeed" in the console, then crashed with error "unexpectedly found nil while unwrapping an Optional value". What's the problem with the last line of code?
Upvotes: 1
Views: 143
Reputation: 348
Have you tried replacing
self!.audioRecorder!.stop()
with using the unwrapped constant
recorder.stop()
Upvotes: 1