-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathRecordingViewController.m
More file actions
233 lines (175 loc) · 8.26 KB
/
RecordingViewController.m
File metadata and controls
233 lines (175 loc) · 8.26 KB
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
//
// Copyright (c) 2013 Carson McDonald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#import "RecordingViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "VideoCameraInputManager.h"
@interface RecordingViewController (Private)
- (void)updateProgress:(NSTimer *)timer;
- (void)saveOutputToAssetLibrary:(NSURL *)outputFileURL completionBlock:(void (^)(NSError *error))completed;
@end
// Maximum and minumum length to record in seconds
#define MAX_RECORDING_LENGTH 6.0
#define MIN_RECORDING_LENGTH 2.0
// Set the recording preset to use
#define CAPTURE_SESSION_PRESET AVCaptureSessionPreset640x480
// Set the input device to use when first starting
#define INITIAL_CAPTURE_DEVICE_POSITION AVCaptureDevicePositionBack
// Set the initial torch mode
#define INITIAL_TORCH_MODE AVCaptureTorchModeOff
@implementation RecordingViewController
{
VideoCameraInputManager *videoCameraInputManager;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
NSTimer *progressUpdateTimer;
}
- (void)viewDidLoad
{
videoCameraInputManager = [[VideoCameraInputManager alloc] init];
videoCameraInputManager.maxDuration = MAX_RECORDING_LENGTH;
videoCameraInputManager.asyncErrorHandler = ^(NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.domain delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
};
NSError *error;
[videoCameraInputManager setupSessionWithPreset:CAPTURE_SESSION_PRESET
withCaptureDevice:INITIAL_CAPTURE_DEVICE_POSITION
withTorchMode:INITIAL_TORCH_MODE
withError:&error];
if(error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.domain delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:videoCameraInputManager.captureSession];
self.videoPreviewView.layer.masksToBounds = YES;
captureVideoPreviewLayer.frame = self.videoPreviewView.bounds;
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.videoPreviewView.layer insertSublayer:captureVideoPreviewLayer below:self.videoPreviewView.layer.sublayers[0]];
// Start the session. This is done asychronously because startRunning doesn't return until the session is running.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[videoCameraInputManager.captureSession startRunning];
});
self.busyView.frame = CGRectMake(self.busyView.frame.origin.x, -self.busyView.frame.size.height, self.busyView.frame.size.width, self.busyView.frame.size.height);
self.saveButton.hidden = YES;
}
[super viewDidLoad];
}
- (void)updateProgress:(NSTimer *)timer
{
CMTime duration = [videoCameraInputManager totalRecordingDuration];
self.videoRecrodingProgress.progress = CMTimeGetSeconds(duration) / MAX_RECORDING_LENGTH;
if(CMTimeGetSeconds(duration) >= MIN_RECORDING_LENGTH)
{
self.saveButton.hidden = NO;
}
if(CMTimeGetSeconds(duration) >= MAX_RECORDING_LENGTH)
{
self.recordButton.enabled = NO;
}
}
- (void)viewDidAppear:(BOOL)animated
{
captureVideoPreviewLayer.frame = self.videoPreviewView.bounds;
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark Record button
- (IBAction)reverseAction:(id)sender {
[videoCameraInputManager reverseCamera];
}
- (IBAction)cancelAction:(id)sender
{
self.saveButton.hidden = YES;
self.videoRecrodingProgress.progress = 0.0;
[videoCameraInputManager reset];
}
- (IBAction)recordTouchDown:(id)sender
{
progressUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.05
target:self
selector:@selector(updateProgress:)
userInfo:nil
repeats:YES];
if(videoCameraInputManager.isPaused)
{
[videoCameraInputManager resumeRecording];
}
else
{
[videoCameraInputManager startRecording];
}
}
- (IBAction)recordTouchCancel:(id)sender
{
[progressUpdateTimer invalidate];
[videoCameraInputManager pauseRecording];
}
- (IBAction)recordTouchUp:(id)sender
{
[progressUpdateTimer invalidate];
[videoCameraInputManager pauseRecording];
}
- (IBAction)saveRecording:(id)sender
{
self.saveButton.hidden = YES;
self.busyView.hidden = NO;
[UIView animateWithDuration:0.25 animations:^{
self.busyView.frame = CGRectMake(self.busyView.frame.origin.x, 0, self.busyView.frame.size.width, self.busyView.frame.size.height);
}];
NSURL *finalOutputFileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@-%ld.mp4", NSTemporaryDirectory(), @"final", (long)[[NSDate date] timeIntervalSince1970]]];
[videoCameraInputManager finalizeRecordingToFile:finalOutputFileURL
withVideoSize:self.videoPreviewView.frame.size
withPreset:AVAssetExportPreset640x480
withCompletionHandler:^(NSError *error) {
if(error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.domain delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
[self saveOutputToAssetLibrary:finalOutputFileURL completionBlock:^(NSError *saveError) {
[UIView animateWithDuration:0.25 animations:^{
self.busyView.frame = CGRectMake(self.busyView.frame.origin.x, -self.busyView.frame.size.height, self.busyView.frame.size.width, self.busyView.frame.size.height);
} completion:^(BOOL finished) {
self.busyView.hidden = YES;
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Done" message:@"The video has been saved to your camera roll." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
});
}];
[[NSFileManager defaultManager] removeItemAtURL:finalOutputFileURL error:nil];
self.videoRecrodingProgress.progress = 0.0;
self.recordButton.enabled = YES;
}];
}
}];
}
- (void)saveOutputToAssetLibrary:(NSURL *)outputFileURL completionBlock:(void (^)(NSError *error))completed
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
completed(error);
}];
}
@end