Reputation: 1622
I'm working on this piece of code responsible for starting video recording on button click. When I click the button camera view is opening but .startVideoCapture() function doesn't start recording.
I get this strange output every time button is pressed:
2015-08-19 16:48:09.588 Record Video With Swift[922:227442] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Also, is there any way to hide the camera view and instead of it place there some progress bar or something?
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)
}
}
Upvotes: 2
Views: 1890
Reputation: 389
I recently had this problem and in my case I was using Swift 3 with iOS 10. There are couple of ways to solve it:
You can call the startVideoCapture() in the completion block of present viewcontroller as following:
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)
}
})
Another way to do is, is by calling startVideoCapture() only after the AVCaptureSessionDidStartRunning notification is fired.
Following is the code to observe the notification:
NotificationCenter.default.addObserver(self, selector: #selector(self.cameraIsReady), name: .AVCaptureSessionDidStartRunning, object: nil)
Following is function called by the above notification:
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)
}
}
}
Basically make sure that your device is ready to capture the video. Hope this is helpful!
Upvotes: 2
Reputation: 990
Try to change following:
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)
}
Upvotes: 0