在使用Swift录制视频时,我做错了什么?

时间:2014-06-25 22:28:05

标签: swift avfoundation ios8

我正在快速录制带有AVFoundation的视频,但我没有看到file.mp4。我不知道我是否录音,而且我保存不好或我没有录音..因为我可以显示会话预览,所有组件都能正常运行..我的代码是:

 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

  }

我无法看到任何明显的错误?

谢谢!

2 个答案:

答案 0 :(得分:4)

AVCaptureFileOutput -startRecordingToOutputFileURL需要NSURL。在发布的代码中,传递的参数filePathString。请尝试使用NSURL(string: filePath)

答案 1 :(得分:1)

我认为您应该检查底部的功能。 AVCaptureFileOutputRecordingDelegate需要

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: AnyObject[]!, error: NSError!) 

正确实施。