-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecordingViewController.m
407 lines (325 loc) · 13.8 KB
/
RecordingViewController.m
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// RecordingViewController.m
// UCSF Book Club
//
// Created by David Chiles on 3/16/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "RecordingViewController.h"
@interface RecordingViewController ()
@end
@implementation RecordingViewController
@synthesize makeAudioButton,uploadButton,deleteButton;
@synthesize infoTableView;
@synthesize fileInfo;
@synthesize content;
@synthesize HUD;
@synthesize dropbox;
@synthesize hudIsVisible;
- (DBRestClient *)restClient {
if (!restClient) {
restClient =
[[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
numberFinished = 0;
}
return self;
}
-(void) viewWillAppear:(BOOL)animated
{
CGSize size = CGSizeMake(320, 460); // size of view in popover
self.contentSizeForViewInPopover = size;
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishedCompression) name:@"finishedCompression" object:nil];
//self.navigationItem.title=[NSString stringWithFormat:@"%d", [fileInfo getNumberOfParticipants]];
[fileInfo getInfo];
int hour = floor(fileInfo.length/3600);
int minT = floor((fileInfo.length-hour*3600)/600);
int minU = floor((fileInfo.length-minT*600-hour*3600)/60);
int secT = floor((fileInfo.length-(minU*60)-(minT*600)-hour*3600)/10);
int secU = fileInfo.length-(minU*60)-(minT*600)-(secT*10)-(hour*3600);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString * startDate = [dateFormatter stringFromDate:fileInfo.startDate];
NSString * length = [NSString stringWithFormat:@"%d:%d%d:%d%d",hour,minT,minU,secT,secU];
NSString * participants = [NSString stringWithFormat:@"%d",fileInfo.numberOfParticipants];
content = [[NSMutableArray alloc]initWithCapacity:3];
[content addObject:[[NSDictionary alloc] initWithObjectsAndKeys:startDate,@"detailTextLabel",@"Start Time",@"textLabel", nil]];
[content addObject:[[NSDictionary alloc] initWithObjectsAndKeys:length,@"detailTextLabel",@"Duration",@"textLabel", nil]];
[content addObject:[[NSDictionary alloc] initWithObjectsAndKeys:participants,@"detailTextLabel",@"Participants",@"textLabel", nil]];
NSMutableDictionary * textDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Text File",@"name", nil];
NSMutableDictionary * micDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Mic Audio",@"name", nil];
//NSMutableDictionary * numDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Number Audio",@"name", nil];
NSMutableDictionary * mixDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Mixed Audio",@"name", nil];
uploadType = [NSArray arrayWithObjects:textDictionary,micDictionary,mixDictionary, nil];
self.navigationItem.title= [fileInfo getName];
[self checkUploadButton];
hudIsVisible = NO;
}
- (void) makeAudio
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
[HUD show:YES];
hudIsVisible = YES;
NSString * audioFilePath = [[[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingString:@"_mixed"] stringByAppendingPathExtension:@"m4a"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:audioFilePath];
if (fileExists) {
NSLog(@"Audio File Exists");
HUD.minShowTime = 2.0;
HUD.labelText = @"Files Created";
//emailPath = checkAudioPath;
numberFinished = 3;
[self finishedCompression];
}
else
{
NSLog(@"create Audio");
//NSString * audioFileName = [fileInfo.name stringByAppendingString:@".caf"];
//NSString * cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//NSString * audioFilePath = [cachesDirectory stringByAppendingPathComponent:audioFileName];
HUD.labelText = @"Creating...";
dispatch_queue_t q = dispatch_queue_create("queue", NULL);
dispatch_async(q, ^{
UBCAudioMixer * audioMixer = [[UBCAudioMixer alloc] init];
[audioMixer makeAllFilesfrom:fileInfo.name];
/*
[UBCAudioMixer audioFilefromText:fileInfo.filePath toFile:audioFilePath];
[UBCAudioMixer createMixedAudiofromTextAudio:audioFilePath andRecording:[[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"caf"]];
dispatch_async(dispatch_get_main_queue(), ^{
HUD.labelText =@"Compressing...";
UBCAudioMixer * audioMixer = [[UBCAudioMixer alloc] init];
[audioMixer convertForExport:fileInfo.name];
});*/
});
dispatch_release(q);
//UBCAudioMixer * audioMixer = [[UBCAudioMixer alloc] init];
//HUD.labelText =@"Compressing...";
//[audioMixer convertForExport:fileInfo.name];
NSLog(@"Email Path: %@",fileInfo.filePath);
}
}
-(void) finishedCompression
{
//[HUD hide:YES];
numberFinished++;
if (numberFinished >= 2)
{
[UBCFileReader deleteCachedFilesRelatedTo:fileInfo.name];
//NSString * localPath = fileInfo.filePath;
//NSString * audioPath = [[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"m4a"];
//NSArray * localPaths = [[NSArray alloc] initWithObjects:localPath,audioPath, nil];
NSString * destinationPath = @"/Recordings/";
if([[NSUserDefaults standardUserDefaults] objectForKey:@"dropbox_path"])
{
destinationPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"dropbox_path"];
}
NSLog(@"destination path: %@",destinationPath);
//[[self restClient] uploadFile:fileName toPath:destinationPath withParentRev:nil fromPath:localPath];
//[[self restClient] loadMetadata:[destinationPath stringByAppendingPathComponent:fileName]];
//[[self restClient] loadMetadata:destinationPath];
if([[DBSession sharedSession] isLinked])
{
self.dropbox = [[UBCDropboxController alloc] init];
dropbox.delegate = self;
if ([fileInfo.uploadList count] > 0) {
HUD.labelText = @"Uploading...";
[dropbox uploadWithFiles:fileInfo.uploadList andDestinationFolder:destinationPath];
}
else {
[HUD hide:YES];
hudIsVisible = NO;
NSLog(@"Nothing to Upload");
}
}
else {
[HUD hide:YES];
hudIsVisible = NO;
NSLog(@"Nothing to Upload");
}
[self checkUploadButton];
}
}
- (IBAction) uploadPressed:(UIButton *)sender
{
int selected = 0;
for(NSDictionary * cellDictionary in uploadType)
{
selected += [[cellDictionary objectForKey:@"selected"] intValue] ;
}
if (selected == 0)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Error"
message: @"You need to select at least one file"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else {
for( int i = 0; i<[uploadType count]; i++)
{
if([[uploadType objectAtIndex:i] objectForKey:@"selected"])
{
switch (i) {
case 0:
//Text File
NSLog(@"Selected: %@",[[uploadType objectAtIndex:i] objectForKey:@"name"]);
[fileInfo.uploadList addObject:fileInfo.filePath];
break;
case 1:
//Mic Audio
NSLog(@"Selected: %@",[[uploadType objectAtIndex:i] objectForKey:@"name"]);
[fileInfo.uploadList addObject:[[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingString:@"_mic.m4a"]];
break;
case 2:
//Mixed Audio
NSLog(@"Selected: %@",[[uploadType objectAtIndex:i] objectForKey:@"name"]);
[fileInfo.uploadList addObject:[[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingString:@"_mixed.m4a"]];
break;
default:
break;
}
}
}
[self makeAudio];
}
}
- (IBAction) deletePressed:(UIButton *)sender
{
}
#pragma Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
return [content count];
}
else if (section == 1)
{
return [uploadType count];
}
return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 1)
{
return @"Choose files to upload ...";
}
return @"";
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifierCheck = @"checkCell";
UITableViewCell *cell;
if (indexPath.section == 0)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSDictionary * text = [content objectAtIndex:indexPath.row];
cell.textLabel.text=[text objectForKey:@"textLabel"];
cell.detailTextLabel.text = [text objectForKey:@"detailTextLabel"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if (indexPath.section == 1)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierCheck];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifierCheck];
}
cell.textLabel.text = [[uploadType objectAtIndex:indexPath.row] objectForKey:@"name"];
}
//cell.textLabel.text = [fileList objectAtIndex:indexPath.row];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1)
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
[[uploadType objectAtIndex:indexPath.row] removeObjectForKey:@"selected"];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
else {
[[uploadType objectAtIndex:indexPath.row] setObject:[NSNumber numberWithInt:1] forKey:@"selected"];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
NSLog(@"Upload List: %@",uploadType);
[self checkUploadButton];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void) checkUploadButton
{
NSString * mixedAudioFilePath = [[[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingString:@"_mixed"] stringByAppendingPathExtension:@"m4a"];
NSString * micAudioFilePath = [[[fileInfo.filePath stringByDeletingPathExtension] stringByAppendingString:@"_mic"] stringByAppendingPathExtension:@"m4a"];
BOOL mixedfileExists = [[NSFileManager defaultManager] fileExistsAtPath:mixedAudioFilePath];
BOOL micfileExists = [[NSFileManager defaultManager] fileExistsAtPath:micAudioFilePath];
if (mixedfileExists && micfileExists && [[DBSession sharedSession] isLinked]) {
//FILES ALREADY EXIST // ONLY NEED TO UPLOAD
[uploadButton setTitle:@"Upload to Dropbox" forState:UIControlStateNormal];
}
else if([[DBSession sharedSession] isLinked] ) {
[uploadButton setTitle:@"Create Audio Files and Upload" forState:UIControlStateNormal];
}
else {
[uploadButton setTitle:@"Create Audio Files" forState:UIControlStateNormal];
}
uploadButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma Dropbox
-(void)uploadsFinished:(int)num
{
[HUD hide:YES];
hudIsVisible = NO;
}
-(void)uploadFialed
{
HUD.labelText = @"Failed";
#ifdef __BLOCKS__
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do a taks in the background
sleep(2);
// Hide the HUD in the main tread
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
hudIsVisible = NO;
});
});
#endif
}
@end