EZAudio不起作用:创建EZRecorder实例时Thread1 EXC_BAD_ACCESS

时间:2016-12-12 15:03:55

标签: ios swift ezaudio

我对EZAudio的完整实施:

class ViewController: UIViewController, EZMicrophoneDelegate, EZRecorderDelegate {

    @IBOutlet var recordingAudioPlot: EZAudioPlot!

    private var isRecording = false {

        didSet {

            if isRecording {

                player.pause()
                recordingAudioPlot.clear()
                microphone.startFetchingAudio()
                recorder = EZRecorder(url: filePathUrl(), clientFormat: microphone.audioStreamBasicDescription(), fileType: EZRecorderFileType.M4A, delegate: self) 
                // ** Here is where the error occurs **

            } else {

                recorder.delegate = nil
                microphone.stopFetchingAudio()
                recorder.closeAudioFile()

                player.playAudioFile(EZAudioFile(url: filePathUrl()))
            }
        }
    }
    private var microphone = EZMicrophone()
    private var recorder = EZRecorder()
    private var player = EZAudioPlayer()

    @IBAction func startStopRecordingButtonTapped(_ sender: UIButton) {
        isRecording = !isRecording
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let session = AVAudioSession.sharedInstance()
        try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try! session.setActive(true)

        microphone.delegate = self

        try! session.overrideOutputAudioPort(.speaker)
    }

    func microphone(_ microphone: EZMicrophone!, hasAudioReceived buffer: UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32) {

        DispatchQueue.main.async {
            self.recordingAudioPlot.updateBuffer(buffer[0], withBufferSize: bufferSize)
        }
    }

    func microphone(_ microphone: EZMicrophone!, hasBufferList bufferList: UnsafeMutablePointer<AudioBufferList>!, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32) {

        if isRecording {
            recorder.appendData(from: bufferList, withBufferSize: bufferSize)
        }
    }

    private func filePathUrl() -> URL {

        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first ?? ""

        return URL(fileURLWithPath: String(format: "%@/%@", path, "pathtofile.m4a"))
    }
}

错误如下:

enter image description here 出了什么问题?

1 个答案:

答案 0 :(得分:0)

解决方案是将recorder声明为可选类型,而不是实例:

private var recorder: EZRecorder?

当第一次尝试释放第一个初始化的记录器时会发生一些事情...但现在有nil因此错误不再存在。