captureOutput:didFinishRecordingToOutputFileAtURL未被调用

时间:2015-03-04 08:37:10

标签: video delegates avfoundation movie video-recording

我是一名新的IOS开发人员。我创建了一个简单的应用程序,使用后置摄像头录制视一切看起来都很好,但函数captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!)从未被调用过。

我期待你的帮助,谢谢!对不起,我的英语不好。 这是我的全部代码:

    import UIKit
import AVFoundation

class MovieRecordingController: UIViewController, AVCaptureFileOutputRecordingDelegate{
    @IBOutlet var frameForCapture: UIView!
    @IBOutlet var buttonRecord: UIButton!

let aVCaptureDevicePosition = AVCaptureDevicePosition.Back
let captureSession = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?
var captureAudioDevice: AVCaptureDevice?
var fileOutput : AVCaptureMovieFileOutput!

override func viewDidLoad() {
    super.viewDidLoad()
    captureSession.sessionPreset = AVCaptureSessionPreset640x480
    let devices = AVCaptureDevice.devices()
    for device in devices {
        if (device.hasMediaType(AVMediaTypeVideo)) {
            if(device.position == aVCaptureDevicePosition) {
                captureDevice = device as? AVCaptureDevice
            }
        }else if device.hasMediaType(AVMediaTypeAudio){
            captureAudioDevice = device as? AVCaptureDevice
        }
    }
    if captureDevice != nil && captureAudioDevice != nil{
        beginSession()
    }else{
        println("Video or Audio device not found")
    }
}

func beginSession() {
    configureDevice()

    var err : NSError? = nil
    var avCaptureInput = AVCaptureDeviceInput(device: captureDevice, error: &err)
    var audtioCaptureInput = AVCaptureDeviceInput(device: captureAudioDevice, error: &err)

    captureSession.addInput(avCaptureInput)
    captureSession.addInput(audtioCaptureInput)
    if err != nil {
        println("error: \(err?.localizedDescription)")
    }

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    self.view.layer.addSublayer(previewLayer)
    previewLayer?.frame = self.frameForCapture.frame

    fileOutput = AVCaptureMovieFileOutput()
    var maxDuration = CMTimeMakeWithSeconds(10, 30)
    fileOutput.maxRecordedDuration = maxDuration
    fileOutput.minFreeDiskSpaceLimit = 1024 * 2014
    captureSession.startRunning()

    if captureSession.canAddOutput(fileOutput) {
        captureSession.addOutput(fileOutput)
    }else{
        println("Can not add video output")
    }
}

func configureDevice() {
    if let device = captureDevice {
        device.lockForConfiguration(nil)
        if aVCaptureDevicePosition == AVCaptureDevicePosition.Back{
            device.focusMode = .Locked
        }
        device.unlockForConfiguration()
    }
}

var isRecording = false;
@IBAction func record(sender : UIButton) {
    if isRecording {
        isRecording = false
        buttonRecord.setTitle("Record", forState: UIControlState.Normal)
        fileOutput.stopRecording()
    }else{
        isRecording = true
        buttonRecord.setTitle("Stop", forState: UIControlState.Normal)
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

        let documentsDirectory = paths[0] as String

        var filePath:String = "file:/\(documentsDirectory)/abc-xyz.mp4"
        var deleteExistFileErr : NSErrorPointer! = nil
        if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
            NSFileManager.defaultManager().removeItemAtPath(filePath, error: deleteExistFileErr)
        }
        var fileUrl = NSURL(string: filePath)
        self.fileOutput.startRecordingToOutputFileURL(fileUrl, recordingDelegate: self)
    }
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
    println("Started")
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!){
    println("Stoped")
}

}

1 个答案:

答案 0 :(得分:0)

擦除这些行:var maxDuration = CMTimeMakeWithSeconds(10,30)     fileOutput.maxRecordedDuration = maxDuration     fileOutput.minFreeDiskSpaceLimit = 1024 * 2014

我自己并没有声称理解它。