Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What am I doing wrong while record video avfoundation with Swift?

I'm recording video with AVFoundation in swift but I don't see the file.mp4. I don't know if I'm recording and I'm saving bad or I'm not recording.. Because I can show the session preview, all components made his function correctly.. My code is:

 import UIKit
 import AVFoundation

 class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {
 var delegate : AVCaptureFileOutputRecordingDelegate?
 @IBOutlet var imageView : UIImageView
 @IBOutlet var imagePreview : UIView

var session : AVCaptureSession!
var fileOutput : AVCaptureMovieFileOutput!
var previewLayer : AVCaptureVideoPreviewLayer!

func initCamera() -> Bool {
 var cameraBack : AVCaptureDevice?
    var videoIn : AVCaptureDeviceInput

    var devices : NSArray = AVCaptureDevice.devices()
    for device: AnyObject in devices{
        if device.position == AVCaptureDevicePosition.Back{
            cameraBack = device as? AVCaptureDevice
        }
    }
 delegate=self
  var error : NSErrorPointer!
    var deviceInput : AVCaptureInput =      AVCaptureDeviceInput.deviceInputWithDevice(cameraBack, error: error) as AVCaptureInput

 self.session=AVCaptureSession()
    self.session.addInput(deviceInput as AVCaptureInput)

    self.fileOutput = AVCaptureMovieFileOutput()
    self.session.addOutput(self.fileOutput)
 var previewLayer: AVCaptureVideoPreviewLayer =     AVCaptureVideoPreviewLayer.layerWithSession(self.session) as AVCaptureVideoPreviewLayer
    previewLayer.frame = self.imagePreview.bounds
    self.imagePreview.layer.addSublayer(previewLayer)

    self.session.startRunning()

 return true
}


  @IBAction func stop(sender : UIButton) {
    println("Stop")
    self.fileOutput.stopRecording()
}

  @IBAction func record(sender : UIButton) {

    var formatter: NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
    let dateTimePrefix: String = formatter.stringFromDate(NSDate())

        let paths = NSSearchPathForDirectoriesInDomains(
            .DocumentDirectory, .UserDomainMask, true)

        let documentsDirectory = paths[0] as String

        var filePath:String? = nil
        var fileNamePostfix = 0
        do {
            filePath =
            "\(documentsDirectory)/\(dateTimePrefix)-\(fileNamePostfix++).mp4"
        } while (NSFileManager.defaultManager().fileExistsAtPath(filePath))
    self.fileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: delegate)

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.initCamera()
}


func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: AnyObject[]!, error: NSError!){
    recording=true
}

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

  }

There are any remarkable error that I can't see??

Thanks!!

like image 968
user3745888 Avatar asked Jun 25 '14 22:06

user3745888


1 Answers

AVCaptureFileOutput -startRecordingToOutputFileURL takes an NSURL. In the code posted, the passed parameter filePath is a String. Try using NSURL(string: filePath) instead.

like image 65
mattt Avatar answered Sep 29 '22 05:09

mattt