简体   繁体   中英

How to set a 10 second limit to video recording and in real time remove the 10 seconds old film parts - Swift iOS

I can understand if the title is really confusing, but I'm going to do my best to explain more in detail! I have plans on an iOS Swift app that will record a video with a time limit of 10 seconds. But when the clip reaches 10 seconds, it should not stop recording. It should remove the end of the clip, so it never gets longer than 10 seconds. So it don't take up so much memory. And when someone presses the "stop-recording-button" it saves it to the camera roll.

I've searched the web for something like this, but I haven't fount anything helpful. Do you know a good tutorial or even a source code?

//Thanks in advance, Anton

使用属性:

videoMaximumDuration

You have a kinda confusing question. You don't want to stop recording but you just want to remove the end of the clip? Why not just stop the recording after 10 seconds?

You can use dispatch_time to stop, works for me:

func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {

    captureOutput.maxRecordedDuration = CMTimeMake(10, 1)

    /* After 10 seconds, let's stop the recording process */
    let delayInSeconds = 10.0
    let delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
    dispatch_after(delayInNanoSeconds, dispatch_get_main_queue(), {
        captureOutput.stopRecording()
    })

    return
}

If recording using AVFoundation should set maximum recorded duration when adding AVCaptureMovieFileOutput to AVCaptureSession .

private let session = AVCaptureSession()
private let movieOutput = AVCaptureMovieFileOutput()

movieOutput.maxRecordedDuration = CMTime(seconds: 10, preferredTimescale: 600)
if session.canAddOutput(movieOutput) {
    session.addOutput(movieOutput)
}

After max recording duration fileOutput(didFinishRecordingTo:from:error:) will be called with your video saved to specified URL with duration some milliseconds below max. Note that error will not be nil.

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