在使用Swift录制视频时

在使用Swift录制视频时

本文介绍了在使用Swift录制视频时,我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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??

谢谢!!

推荐答案

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

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

这篇关于在使用Swift录制视频时,我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:53