Swift单键处理音频记录,暂停和恢复

时间:2018-11-07 17:58:02

标签: swift button avfoundation

我正试图在audio框架的帮助下创建record pauseresumeAVFoundation记录。在这里,我已经完成了记录并停止使用单个button,但是我想执行单个按钮处理记录,暂停和恢复三种功能。该怎么做?

@IBAction func record_click(_ sender: Any) {

        // Record, Pause and Resume need to handle here

        if let audioRecorder = self.audioRecorder {
            if (audioRecorder.isRecording) {
                audioRecorder.stop()
                record_button.setImage(UIImage(named: "play.png"), for: UIControlState.normal)
                save_bar_button.isEnabled = true

            } else {

                audioRecorder.record()
                recordTimer = Timer.scheduledTimer(timeInterval: 0.1, target:self, selector:#selector(self.updateAudioMeter(timer:)), userInfo:nil, repeats:true)
                record_button.setImage(UIImage(named: "pause.png"), for: UIControlState.normal)
                save_bar_button.isEnabled = false
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

@IBAction func record_click(_ sender: Any) {

    // Record, Pause and Resume need to handle here

    if let audioRecorder = self.audioRecorder {
        if (audioRecorder.isRecording) {
            audioRecorder.pauseRecording()
            record_button.setImage(UIImage(named: "play.png"), for: UIControlState.normal)
            save_bar_button.isEnabled = true

        } else {

            audioRecorder.resumeRecording()
            recordTimer = Timer.scheduledTimer(timeInterval: 0.1, target:self, selector:#selector(self.updateAudioMeter(timer:)), userInfo:nil, repeats:true)
            record_button.setImage(UIImage(named: "pause.png"), for: UIControlState.normal)
            save_bar_button.isEnabled = false
        }
    }
}

尝试一下