Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 4-覆盖音频录像机录制_Ios_Swift - Fatal编程技术网

Ios Swift 4-覆盖音频录像机录制

Ios Swift 4-覆盖音频录像机录制,ios,swift,Ios,Swift,我正在使用AvAudioRecorder for在iOS应用程序中录制音频。我可以进行初始录制,但我想给用户重新录制的选项 我定义了这些: var recordingSession: AVAudioSession! var audioRecorder: AVAudioRecorder! 在我的视图中,我有: recordingSession = AVAudioSession.sharedInstance() do { try recordingSe

我正在使用AvAudioRecorder for在iOS应用程序中录制音频。我可以进行初始录制,但我想给用户重新录制的选项

我定义了这些:

var recordingSession: AVAudioSession!

var audioRecorder: AVAudioRecorder!
在我的视图中,我有:

recordingSession = AVAudioSession.sharedInstance()

        do {

            try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)

            try recordingSession.setActive(true)

            recordingSession.requestRecordPermission() { result in

                DispatchQueue.main.async {

                    if(result == true)
                    {

                    }
                    else
                    {

                    }

                }

            }

        }
        catch
        {

        }
然后我有我的记录按钮:

@IBAction func recordButtonPressed(_ sender: Any) {

        if audioRecorder == nil {
            startRecording()
        } else {
            finishRecording(success: true)
        }

    }
以下是我开始录制和完成录制的方法:

func startRecording() {

        audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a")

        let settings = [
            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
            AVSampleRateKey: 12000,
            AVNumberOfChannelsKey: 1,
            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
        ]

        do {

            audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
            audioRecorder.delegate = self
            audioRecorder.record()

            recordButton.setTitle("Tap to Stop", for: .normal)

        } catch {

            finishRecording(success: false)

        }
    }

    func finishRecording(success: Bool) {

        audioRecorder.stop()
        audioRecorder = nil

        if success {
            recordButton.setTitle("Tap to Re-record", for: .normal)

            audioSlider.isHidden = false
            playButton.isHidden = false
            uploadButton.isHidden = false

            do {

                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                try AVAudioSession.sharedInstance().setActive(true)

                self.audioPlayer = try AVAudioPlayer(contentsOf: audioFilename)
                self.audioPlayer?.prepareToPlay()

            }catch{

            }


        } else {
            recordButton.setTitle("Tap to Record", for: .normal)
        }
    }

问题是无论我试图重新录制什么,它都不会。录音结束后,我尝试将录音机设置为零,但没有任何帮助。

这不是答案,但为什么要设置为“激活”(true)在finish recording plus中,一旦开始录制,您就不会停止录制。您需要检查录制是否成功,而不是检查是否为零。在尝试重新录制音频时,我遇到了一个案例AVAudioRecorder将新音频附加到现有文件中。尽管文档中说.record()方法应该覆盖文件(如果存在):