iOS 8 iPad AVCaptureMovieFileOutput drops / loses / never gets audio track after 13 - 14 seconds of recording

前端 未结 2 1270
别跟我提以往
别跟我提以往 2021-01-01 18:07

I have the following code which works for iOS 6 & 7.x.

In iOS 8.1 I have a strange issue where if you capture a session for about 13 seconds or longer, the resu

2条回答
  •  情话喂你
    2021-01-01 18:47

    I had this issue and the way to fix this in Swift 4 is the following:

    • Do not set movieFileOutput.maxRecordedDuration. There seems to be a bug with this where if you set this then if you are recording videos for longer than 12-13 seconds they will have no audio.

    • Instead use a timer to stop the recording and set movieFragmentInterval like this:

    movieFileOutput.movieFragmentInterval = CMTime.invalid

    Here is a whole block of code just to show you how I did it:

    var seconds = 20
    var timer = Timer()
    var movieFileOutput = AVCaptureMovieFileOutput()
    
    func startRecording(){
        movieFileOutput.movieFragmentInterval = CMTime.invalid
        movieFileOutput.startRecording(to: URL(fileURLWithPath: getVideoFileLocation()), recordingDelegate: self)
        startTimer()
    }
    
    func stopRecording(){
        movieFileOutput.stopRecording()
        timer.invalidate()
    }
    
    func startTimer(){
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
    }
    
    @objc func updateTimer(){
        seconds -= 1
        if(seconds == 0){
            stopRecording()
        }
    }
    
    func getVideoFileLocation() -> String {
        return NSTemporaryDirectory().appending("myrecording.mp4")
    }
    
    
    extension FTVideoReviewViewController : AVCaptureFileOutputRecordingDelegate{
        public func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
            print("Finished recording: \(outputFileURL)")
            // do stuff here when recording is finished
        }
    }
    

提交回复
热议问题