ios - 无法停止录音机

标签 ios xcode swift

部分代码:

类 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. 
                })

        } 
    }
}

它在控制台中打印“捕获成功”,然后因错误“在展开可选值时意外发现 nil”而崩溃。 最后一行代码有什么问题?

最佳答案

你试过更换

self!.audioRecorder!.stop()

使用展开常量

recorder.stop()

关于ios - 无法停止录音机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928710/

相关文章:

ios - "Unable to download application. <Appname> could not be installed at this time"

xcode - 在 Swift 2 中为特定时间安排本地通知

ios - SpriteKit 横向方向不起作用

ios - Carthage 更新失败,错误为 'could not posix spawn 35'

iOS 应用内购买 : User Charged Twice for non-consumable?

android - Ionic2 - Google map 插件,错误 : EXCEPTION: Uncaught (in promise): [object Object])

ios - 如何将多个选定的 UISwitch tableViewCell 行中的数组传递到下一个 View Controller

ios - 代码 "Message from debugger: got unexpected response to k packet: OK"

ios - 如何在 NWConnection 中设置 UDP 广播的发送方端口

ios - 在 SwiftUI 中检测 DragGesture 取消