简体   繁体   中英

AVAudioRecorder long delay when record() is called

I'm using AVAudioRecorder to record audio within a video. When I press 'start' button to start recording there is a long delay, sometimes as much as 6 seconds before the audio recording starts. The recording is then not successful and appears in the file with a duration of 0.0 secs.

**** Post has been edited to try make problem clearer

I've created a sample audio recording project where all I do is try to record the audio and it's still not working for me. My code is as follows:

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioRecorderDelegate {

    var recordingSession: AVAudioSession!
    var audioRecorder: AVAudioRecorder!
    var settings = [String : Int]()
    var outputAudioLocation: URL!

    @IBOutlet weak var startButton: UIButton!
    @IBOutlet weak var stopButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        //setUpAudioSession()

        recordingSession = AVAudioSession.sharedInstance()
        do {
            try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try recordingSession.setActive(true)
            recordingSession.requestRecordPermission { (hasPermission) in
            if hasPermission {
                print("Accepted")
            } else {
                print("Not allowed")
            }
        }
            print("audio set up")
        } catch {
            print("Recording session error")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func startButtonPressed(_ sender: Any) {
        recordAudio()
    }

    @IBAction func stopButtonPressed(_ sender: Any) {
        stopRecordingAudio()
    }

    func recordAudio() {
        // Check if we have an active recorder
        if audioRecorder == nil {

            outputAudioLocation = getDirectory().appendingPathComponent("audio.m4a")

            settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                        AVSampleRateKey: 44100,
                        AVNumberOfChannelsKey: 2,
                        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
            ]

            // Start Audio Recording
            do {
                audioRecorder = try AVAudioRecorder(url: outputAudioLocation, settings: settings)
                audioRecorder.delegate = self
                audioRecorder.record()
                print("audio recording")
            } catch {
                print("Audio recording hasn't worked")
            }
        }
    }

    func stopRecordingAudio() {
        if audioRecorder != nil {
            audioRecorder.stop()
            print("audio stopped recording")
            audioRecorder = nil
        }
    }

    //directory for audio recording
    func getDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = paths[0]
        return documentDirectory
    }


    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        if !flag {
            print("audio recording successful")
        } else {
            print("audio recording not successful")
        }
    }

}

**** Update

I've downloaded a GitHub project that uses AVAudioRecorder and when I try it on my phone which is an iPhone 6 the recording doesn't work and saves with a duration of 0.0. However, when I use the simulator (iPhone 8) it works as expected. Does anyone have any idea why this might be happening on my phone? With my own project it was working fine until today.

After wasting over 5 hours of my life trying to work out what was wrong with my code it turns out it was a problem with my iPhone.

I turned my phone off and on again and the recording is now working. I never thought that would be the problem.

Thanks @matt for trying to help me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM