Commit 61a63660 by Daniel Dahan

updated CaptureView example to display video recording duration

parent 04fa6ab0
...@@ -35,7 +35,9 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -35,7 +35,9 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
private lazy var flashButton: FlatButton = FlatButton() private lazy var flashButton: FlatButton = FlatButton()
private lazy var closeButton: FlatButton = FlatButton() private lazy var closeButton: FlatButton = FlatButton()
private lazy var captureMode: CaptureMode = .Photo private lazy var captureMode: CaptureMode = .Video
private var timer: NSTimer?
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
...@@ -66,6 +68,24 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -66,6 +68,24 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
navigationBarView.shadowDepth = .None navigationBarView.shadowDepth = .None
navigationBarView.statusBarStyle = .LightContent navigationBarView.statusBarStyle = .LightContent
// Title label.
let titleLabel: UILabel = UILabel()
titleLabel.text = "00:00:00"
titleLabel.textAlignment = .Left
titleLabel.textColor = MaterialColor.white
titleLabel.font = RobotoFont.regularWithSize(20)
navigationBarView.titleLabel = titleLabel
navigationBarView.titleLabelInsetsRef.left = 64
// Detail label
let detailLabel: UILabel = UILabel()
detailLabel.text = "Stopped"
detailLabel.textAlignment = .Left
detailLabel.textColor = MaterialColor.white
detailLabel.font = RobotoFont.regularWithSize(12)
navigationBarView.detailLabel = detailLabel
navigationBarView.detailLabelInsetsRef.left = 64
view.addSubview(navigationBarView) view.addSubview(navigationBarView)
navigationBarView.leftButtons = [closeButton] navigationBarView.leftButtons = [closeButton]
navigationBarView.rightButtons = [switchCameraButton, flashButton] navigationBarView.rightButtons = [switchCameraButton, flashButton]
...@@ -77,7 +97,7 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -77,7 +97,7 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
private func prepareCaptureButton() { private func prepareCaptureButton() {
captureButton.pulseColor = MaterialColor.white captureButton.pulseColor = MaterialColor.white
captureButton.pulseFill = true captureButton.pulseFill = true
captureButton.backgroundColor = MaterialColor.blue.darken1.colorWithAlphaComponent(0.3) captureButton.backgroundColor = MaterialColor.red.darken1.colorWithAlphaComponent(0.3)
captureButton.borderWidth = .Border2 captureButton.borderWidth = .Border2
captureButton.borderColor = MaterialColor.white captureButton.borderColor = MaterialColor.white
captureButton.shadowDepth = .None captureButton.shadowDepth = .None
...@@ -179,8 +199,10 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -179,8 +199,10 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
} else if .Video == captureMode { } else if .Video == captureMode {
if captureView.captureSession.isRecording { if captureView.captureSession.isRecording {
captureView.captureSession.stopRecording() captureView.captureSession.stopRecording()
stopTimer()
} else { } else {
captureView.captureSession.startRecording() captureView.captureSession.startRecording()
startTimer()
} }
} }
} }
...@@ -207,6 +229,7 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -207,6 +229,7 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
:name: handleFlashButton :name: handleFlashButton
*/ */
internal func handleFlashButton(button: UIButton) { internal func handleFlashButton(button: UIButton) {
if .Back == captureView.captureSession.cameraPosition {
var img: UIImage? var img: UIImage?
switch captureView.captureSession.flashMode { switch captureView.captureSession.flashMode {
...@@ -224,6 +247,7 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -224,6 +247,7 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
flashButton.setImage(img, forState: .Normal) flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted) flashButton.setImage(img, forState: .Highlighted)
} }
}
/** /**
:name: handleCameraButton :name: handleCameraButton
...@@ -231,6 +255,8 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -231,6 +255,8 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
func handleCameraButton(button: UIButton) { func handleCameraButton(button: UIButton) {
captureButton.backgroundColor = MaterialColor.blue.darken1.colorWithAlphaComponent(0.3) captureButton.backgroundColor = MaterialColor.blue.darken1.colorWithAlphaComponent(0.3)
captureMode = .Photo captureMode = .Photo
navigationBarView.titleLabel!.text = ""
navigationBarView.detailLabel!.text = ""
} }
/** /**
...@@ -239,6 +265,8 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -239,6 +265,8 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
func handleVideoButton(button: UIButton) { func handleVideoButton(button: UIButton) {
captureButton.backgroundColor = MaterialColor.red.darken1.colorWithAlphaComponent(0.3) captureButton.backgroundColor = MaterialColor.red.darken1.colorWithAlphaComponent(0.3)
captureMode = .Video captureMode = .Video
navigationBarView.titleLabel!.text = "00:00:00"
navigationBarView.detailLabel!.text = "Stopped"
} }
/** /**
...@@ -249,6 +277,30 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -249,6 +277,30 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
} }
/** /**
:name: captureSessionWillSwitchCameras
*/
func captureSessionWillSwitchCameras(capture: CaptureSession, position: AVCaptureDevicePosition) {
if .Back == position {
let img: UIImage? = UIImage(named: "ic_flash_off_white")
captureView.captureSession.flashMode = .Off
flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted)
}
}
/**
:name: captureSessionDidSwitchCameras
*/
func captureSessionDidSwitchCameras(capture: CaptureSession, position: AVCaptureDevicePosition) {
if .Back == position {
let img: UIImage? = UIImage(named: "ic_flash_auto_white")
captureView.captureSession.flashMode = .Auto
flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted)
}
}
/**
:name: captureStillImageAsynchronously :name: captureStillImageAsynchronously
*/ */
func captureStillImageAsynchronously(capture: CaptureSession, image: UIImage) { func captureStillImageAsynchronously(capture: CaptureSession, image: UIImage) {
...@@ -275,5 +327,41 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi ...@@ -275,5 +327,41 @@ class ViewController: UIViewController, CapturePreviewViewDelegate, CaptureSessi
func captureDidFinishRecordingToOutputFileAtURL(capture: CaptureSession, captureOutput: AVCaptureFileOutput, outputFileURL: NSURL, fromConnections connections: [AnyObject], error: NSError!) { func captureDidFinishRecordingToOutputFileAtURL(capture: CaptureSession, captureOutput: AVCaptureFileOutput, outputFileURL: NSURL, fromConnections connections: [AnyObject], error: NSError!) {
print("Capture Stopped Recording \(outputFileURL)") print("Capture Stopped Recording \(outputFileURL)")
} }
/**
:name: startTimer
*/
func startTimer() {
timer?.invalidate()
timer = NSTimer(timeInterval: 0.5, target: self, selector: "updateTimer", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
navigationBarView.detailLabel!.textColor = MaterialColor.red.accent1
captureButton.backgroundColor = MaterialColor.red.darken4.colorWithAlphaComponent(0.3)
}
/**
:name: updateTimer
*/
func updateTimer() {
let duration: CMTime = captureView.captureSession.recordedDuration
let time: Double = CMTimeGetSeconds(duration)
let hours: Int = Int(time / 3600)
let minutes: Int = Int((time / 60) % 60)
let seconds: Int = Int(time % 60)
navigationBarView.titleLabel!.text = String(format: "%02i:%02i:%02i", arguments: [hours, minutes, seconds])
navigationBarView.detailLabel!.text = "Recording"
}
/**
:name: stopTimer
*/
func stopTimer() {
timer?.invalidate()
timer = nil
navigationBarView.titleLabel!.text = "00:00:00"
navigationBarView.detailLabel!.text = "Stopped"
navigationBarView.detailLabel!.textColor = MaterialColor.white
captureButton.backgroundColor = MaterialColor.red.darken1.colorWithAlphaComponent(0.3)
}
} }
...@@ -80,6 +80,16 @@ public protocol CaptureSessionDelegate { ...@@ -80,6 +80,16 @@ public protocol CaptureSessionDelegate {
optional func captureSessionFailedWithError(capture: CaptureSession, error: NSError) optional func captureSessionFailedWithError(capture: CaptureSession, error: NSError)
/** /**
:name: captureSessionDidSwitchCameras
*/
optional func captureSessionDidSwitchCameras(capture: CaptureSession, position: AVCaptureDevicePosition)
/**
:name: captureSessionWillSwitchCameras
*/
optional func captureSessionWillSwitchCameras(capture: CaptureSession, position: AVCaptureDevicePosition)
/**
:name: captureStillImageAsynchronously :name: captureStillImageAsynchronously
*/ */
optional func captureStillImageAsynchronously(capture: CaptureSession, image: UIImage) optional func captureStillImageAsynchronously(capture: CaptureSession, image: UIImage)
...@@ -158,6 +168,13 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -158,6 +168,13 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
public private(set) lazy var isRecording: Bool = false public private(set) lazy var isRecording: Bool = false
/** /**
:name: recordedDuration
*/
public var recordedDuration: CMTime {
return movieOutput.recordedDuration
}
/**
:name: activeCamera :name: activeCamera
*/ */
public var activeCamera: AVCaptureDevice? { public var activeCamera: AVCaptureDevice? {
...@@ -377,8 +394,8 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -377,8 +394,8 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
*/ */
public func switchCameras() { public func switchCameras() {
if canSwitchCameras { if canSwitchCameras {
dispatch_async(videoQueue) {
do { do {
self.delegate?.captureSessionWillSwitchCameras?(self, position: self.cameraPosition)
let videoInput: AVCaptureDeviceInput? = try AVCaptureDeviceInput(device: self.inactiveCamera!) let videoInput: AVCaptureDeviceInput? = try AVCaptureDeviceInput(device: self.inactiveCamera!)
self.session.beginConfiguration() self.session.beginConfiguration()
self.session.removeInput(self.activeVideoInput) self.session.removeInput(self.activeVideoInput)
...@@ -390,12 +407,12 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -390,12 +407,12 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
self.session.addInput(self.activeVideoInput) self.session.addInput(self.activeVideoInput)
} }
self.session.commitConfiguration() self.session.commitConfiguration()
self.delegate?.captureSessionDidSwitchCameras?(self, position: self.cameraPosition)
} catch let e as NSError { } catch let e as NSError {
self.delegate?.captureSessionFailedWithError?(self, error: e) self.delegate?.captureSessionFailedWithError?(self, error: e)
} }
} }
} }
}
/** /**
:name: isFocusModeSupported :name: isFocusModeSupported
...@@ -541,7 +558,6 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -541,7 +558,6 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
:name: startRecording :name: startRecording
*/ */
public func startRecording() { public func startRecording() {
dispatch_async(videoQueue) {
if !self.isRecording { if !self.isRecording {
let connection: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo) let connection: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo)
connection.videoOrientation = self.currentVideoOrientation connection.videoOrientation = self.currentVideoOrientation
...@@ -564,7 +580,6 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -564,7 +580,6 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
} }
} }
} }
}
/** /**
:name: captureOutput :name: captureOutput
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment