-
Notifications
You must be signed in to change notification settings - Fork 6
/
KVideoRecorder.swift
364 lines (264 loc) · 10.9 KB
/
KVideoRecorder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// KVideoRecorder
//
// Copyright © 2017 Kenan Atmaca. All rights reserved.
// kenanatmaca.com
//
//
import UIKit
import AVFoundation
enum CaptureMode {
case photo
case video
}
protocol KVideoRecorderDelegate:class {
func timer(second:Int)
}
@available(iOS 11,*)
class KVideoRecorder: NSObject {
private var captureVideoDevice:AVCaptureDevice!
private var captureAudioDevice:AVCaptureDevice!
private var session:AVCaptureSession!
private var previewLayer:AVCaptureVideoPreviewLayer!
private var videoOutput:AVCaptureMovieFileOutput!
private var photoOutput:AVCapturePhotoOutput!
private var zoomGesture:UIPinchGestureRecognizer!
private var focusGesture:UITapGestureRecognizer!
private var toggleGesture:UITapGestureRecognizer!
private var rootView:UIView!
private var stateZoomScale:CGFloat = 1.0
private var videoTimer:Timer!
private var recordTime:Int = 0
private var captureTyp:CaptureMode!
var isAuth:Bool! {
get {
return auth()
}
}
var isFocus:Bool = false
var isZoom:Bool = true
var isToggle:Bool = true
var videoDelegate:AVCaptureFileOutputRecordingDelegate?
var photoDelegate:AVCapturePhotoCaptureDelegate?
var delegate:KVideoRecorderDelegate?
var takePhotoImage:UIImage?
var videoOutputUrl:URL?
init(to view:UIView) {
super.init()
self.rootView = view
}
func setup(_ type:CaptureMode) {
guard isAuth else {
return
}
captureTyp = type
session = AVCaptureSession()
captureVideoDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
captureAudioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
do {
try captureVideoDevice?.lockForConfiguration()
captureVideoDevice?.focusMode = .continuousAutoFocus
captureVideoDevice?.unlockForConfiguration()
} catch {
print(error.localizedDescription)
}
do {
let inputVideo = try AVCaptureDeviceInput(device: captureVideoDevice)
let inputAudio = try AVCaptureDeviceInput(device: captureAudioDevice)
session.addInput(inputVideo)
session.addInput(inputAudio)
} catch {
print(error.localizedDescription)
}
switch(type) {
case .photo:
photoOutput = AVCapturePhotoOutput()
session.addOutput(photoOutput)
case .video:
videoOutput = AVCaptureMovieFileOutput()
session.addOutput(videoOutput)
}
previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.zPosition = -1
previewLayer.contentsGravity = kCAGravityResizeAspectFill
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.frame = rootView.layer.bounds
rootView.layer.addSublayer(previewLayer)
focusGesture = UITapGestureRecognizer(target: self, action: #selector(focusCam(_:)))
zoomGesture = UIPinchGestureRecognizer(target: self, action: #selector(zoomCamera(_:)))
toggleGesture = UITapGestureRecognizer(target: self, action: #selector(toggleCamera))
toggleGesture.numberOfTapsRequired = 2
if isFocus {rootView.addGestureRecognizer(focusGesture)}
if isZoom {rootView.addGestureRecognizer(zoomGesture)}
if isToggle {rootView.addGestureRecognizer(toggleGesture)}
session.startRunning()
}
func record(name:String = "movie") {
guard videoOutput != nil else {
return
}
if !videoOutput.isRecording {
if case captureTyp = CaptureMode.video {
let outputURL = vidURL(name: name)
videoOutput.startRecording(toOutputFileURL: outputURL, recordingDelegate: videoDelegate ?? self as AVCaptureFileOutputRecordingDelegate)
if delegate != nil {videoTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(setTimerCount), userInfo: nil, repeats: true)}
}
}
}
func savePhoto(image:UIImage) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
func saveVideo(url:URL) {
UISaveVideoAtPathToSavedPhotosAlbum(url.path, nil, nil, nil)
}
@objc private func setTimerCount() {
recordTime += 1
if delegate != nil {
self.delegate?.timer(second: recordTime)
}
}
func takePhoto() {
guard photoOutput != nil else {
return
}
if case captureTyp = CaptureMode.photo {
let settings = AVCapturePhotoSettings()
photoOutput.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecType.jpeg])], completionHandler: nil)
photoOutput.capturePhoto(with: settings, delegate: photoDelegate ?? self as AVCapturePhotoCaptureDelegate)
}
}
func stop() {
guard videoOutput != nil else {
return
}
if videoOutput.isRecording {
videoOutput.stopRecording()
videoTimer.invalidate()
}
}
func toggleCamera(){
var newCamera:AVCaptureDevice?
func cameraState(_ position:AVCaptureDevice.Position) -> AVCaptureDevice? {
let deviceDescoverySession = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera],mediaType: AVMediaTypeVideo,position: AVCaptureDevice.Position.unspecified)
for device in (deviceDescoverySession?.devices)! {
if device.position == position {
return device
}
}
return nil
}
session?.beginConfiguration()
let currentInput = session.inputs.first as! AVCaptureInput!
session.removeInput(currentInput!)
if captureVideoDevice?.position == AVCaptureDevice.Position.back {
newCamera = cameraState(AVCaptureDevice.Position.front)
} else {
newCamera = cameraState(AVCaptureDevice.Position.back)
}
captureVideoDevice = newCamera
do {
let deviceInput = try AVCaptureDeviceInput(device: newCamera!)
session?.addInput(deviceInput)
} catch {
print(error.localizedDescription)
}
session?.commitConfiguration()
}
@objc private func focusCam(_ sender:UITapGestureRecognizer) {
let point = sender.location(in: rootView)
focusObject(point)
}
private func focusObject(_ point:CGPoint){
if let device = captureVideoDevice {
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported {
device.focusPointOfInterest = point
}
if device.isExposurePointOfInterestSupported {
device.exposurePointOfInterest = point
device.exposureMode = .autoExpose
}
device.unlockForConfiguration()
} catch {
print(error.localizedDescription)
}
}
}
@objc private func zoomCamera(_ sender:UIPinchGestureRecognizer){
if let device = captureVideoDevice {
if sender.state == UIGestureRecognizerState.began { sender.scale = stateZoomScale }
if sender.state == UIGestureRecognizerState.ended { stateZoomScale = device.videoZoomFactor }
if sender.scale <= 1 { sender.scale = 1 }
else if sender.scale >= 4 { sender.scale = 4 }
do {
try device.lockForConfiguration()
device.videoZoomFactor = sender.scale
device.unlockForConfiguration()
} catch {
print(error.localizedDescription)
}
}
}
private func getDir() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths.first!
}
private func auth() -> Bool {
let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch (status) {
case .authorized: return true
case .denied,.notDetermined,.restricted : return false
}
}
func isExist(name:String) -> Bool {
let bundle = getDir().appendingPathComponent(name.appending(".mov"))
let manager = FileManager.default
return manager.fileExists(atPath: bundle.path) ? true : false
}
@discardableResult
func delete(name:String) -> Bool {
let bundle = getDir().appendingPathComponent(name.appending(".mov"))
let manager = FileManager.default
var result:Bool = false
if self.isExist(name: name) {
do {
try manager.removeItem(at: bundle)
result = true
} catch {
print(error.localizedDescription)
result = false
}
}
return result
}
func removeView() {
if videoTimer != nil {videoTimer.invalidate()}
session = nil
videoOutputUrl = nil
takePhotoImage = nil
recordTime = 0
previewLayer.removeFromSuperlayer()
}
func vidURL(name:String) -> URL {
return getDir().appendingPathComponent(name.appending(".mov"))
}
}//
@available(iOS 11,*)
extension KVideoRecorder: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imgData = photo.fileDataRepresentation() {
takePhotoImage = UIImage(data: imgData)
}
}
}
@available(iOS 11,*)
extension KVideoRecorder: AVCaptureFileOutputRecordingDelegate {
func capture(_ output: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {}
func capture(_ output: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
if error != nil {
return
}
self.videoOutputUrl = outputFileURL
}
}