使用Swift自动捕获视频

时间:2015-08-19 16:05:58

标签: ios swift avcapturesession

我正在处理这段代码,负责在点击按钮时启动视频录制。 当我单击按钮时相机视图正在打开,但.startVideoCapture()函数无法开始录制。

每按一次按钮,我都会得到这个奇怪的输出:

  

2015-08-19 16:48:09.588用Swift录制视频[922:227442]   快照未渲染的视图会导致空白   快照。确保您的视图至少在之前呈现过一次   屏幕更新后的快照或快照。

此外,有没有办法隐藏相机视图而不是它放置一些进度条或什么?

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate 
    {

       let captureSession = AVCaptureSession()
       var previewLayer : AVCaptureVideoPreviewLayer?
        var captureDevice : AVCaptureDevice?
       var imagePicker = UIImagePickerController()

  override func viewDidAppear(animated: Bool) 
 {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
        println("captureVideoPressed and camera available.")
        imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .Camera;
        imagePicker.mediaTypes = [kUTTypeMovie!]
        imagePicker.allowsEditing = false
        imagePicker.showsCameraControls = true
    }

    else 
    {
        println("Camera not available.")
    }
}

@IBAction func rec(sender: AnyObject) 
{
    self.presentViewController(imagePicker, animated: true, completion: nil)
    imagePicker.startVideoCapture()
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) 
    {
    let tempImage = info[UIImagePickerControllerMediaURL] as! NSURL!
    let pathString = tempImage.relativePath
    self.dismissViewControllerAnimated(true, completion: {})
    UISaveVideoAtPathToSavedPhotosAlbum(pathString, self, nil, nil)
     }
    }

2 个答案:

答案 0 :(得分:2)

我最近有这个问题,在我的情况下,我在iOS 10中使用Swift 3.有几种方法可以解决它:

您可以在当前viewcontroller的完成块中调用startVideoCapture(),如下所示:

self.present(self.cameraController, animated: true, completion: {
    self.cameraController.startVideoCapture()

    if #available(iOS 10.0, *) {
        Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { _ in
            self.stopMovementsCapture()
        }
    } else {
        // Fallback on earlier versions
        Timer.scheduledTimer(timeInterval: 5,
                             target: self,
                             selector: #selector(self.stopMovementsCapture),
                             userInfo: nil,
                             repeats: false)
    }
})

另一种方法是,只有在触发AVCaptureSessionDidStartRunning通知后才调用startVideoCapture()。

以下是观察通知的代码:

NotificationCenter.default.addObserver(self, selector: #selector(self.cameraIsReady), name: .AVCaptureSessionDidStartRunning, object: nil)

以下是上述通知调用的函数:

func cameraIsReady(notification: NSNotification) {
    DispatchQueue.main.async {
        self.cameraController.startVideoCapture()

        if #available(iOS 10.0, *) {
            Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { _ in
                self.stopMovementsCapture()
            }
        } else {
            // Fallback on earlier versions
            Timer.scheduledTimer(timeInterval: 5,
                                 target: self,
                                 selector: #selector(self.stopMovementsCapture),
                                 userInfo: nil,
                                 repeats: false)
        }
    }
}

基本上确保您的设备已准备好捕获视频。希望这有用!

答案 1 :(得分:0)

尝试更改以下内容:

1

imagePicker.mediaTypes = NSArray(object: kUTTypeMovie)  as! [String]

2

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let tempImage = info[UIImagePickerControllerMediaURL] as! NSURL!
    let pathString = tempImage.relativePath

    self.dismissViewControllerAnimated(true, completion: {})

    UISaveVideoAtPathToSavedPhotosAlbum(pathString!, self, nil, nil)

}