-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioSoundboardViewController.m
executable file
·383 lines (285 loc) · 13.8 KB
/
AudioSoundboardViewController.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
//
// AudioListViewController.m
// Field Guide 2010
//
// Created by Simon Sherrin on 27/11/10.
/*
Copyright (c) 2011 Museum Victoria
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 "AudioSoundboardViewController.h"
#import "Audio.h"
#import "Speci.h"
#import "iPhoneDetailViewController.h"
#import "DetailViewController.h"
@implementation AudioSoundboardViewController
@synthesize audioFilesArray;
@synthesize player, fileRowLocations, activeTrack, inactiveTrack;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize rightViewReference = _rightViewReference;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
self.fileRowLocations = [NSMutableDictionary dictionaryWithCapacity:5];
self.tableView.bounces = YES; //Stops scrolling of the contents
inactiveTrack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AudioTrackInactive.png"] highlightedImage:[UIImage imageNamed:@"AudioTrackHighlightInactive.png"]];
UIImage *activeImageNormal = [UIImage imageNamed:@"AudioTrackActive3.png"];
UIImage *activeImageHighlight = [UIImage imageNamed:@"AudioTrackHighlightActive3.png"];
activeTrack = [[UIImageView alloc] initWithImage:activeImageNormal highlightedImage:activeImageHighlight];
//Animation
UIImage *activeImageNormal1 = [UIImage imageNamed:@"AudioTrackActive1.png"];
UIImage *activeImageHighlight1 = [UIImage imageNamed:@"AudioTrackHighlightActive1.png"];
UIImage *activeImageNormal2 = [UIImage imageNamed:@"AudioTrackActive2.png"];
UIImage *activeImageHighlight2 =[UIImage imageNamed:@"AudioTrackHighlightActive2.png"];
NSArray *activeNormalAnimation = [NSArray arrayWithObjects:activeImageNormal1, activeImageNormal2, activeImageNormal, nil];
NSArray *activeHighlightAnimation = [NSArray arrayWithObjects:activeImageHighlight1, activeImageHighlight2, activeImageHighlight, nil];
NSLog(@"Active Normal Animation: %d", [activeNormalAnimation count]);
NSLog(@"Active Highlight Animation: %d ", [activeHighlightAnimation count]);
activeTrack.animationImages = activeNormalAnimation;
activeTrack.highlightedAnimationImages = activeHighlightAnimation;
}
- (CGSize)contentSizeForViewInPopover{
//return self.view.bounds.size;
if ([audioFilesArray count] >0) {
return CGSizeMake(320, [audioFilesArray count]*43);
NSLog(@"Called contentsize for popover");
}else{
return CGSizeMake(320,160);
}
}
- (void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
[super viewWillAppear:animated];
}
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
// return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
tableView.rowHeight = 75;
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"Audio: Number of Rows in Section");
//return [audioFilesArray count];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSLog(@"Audio: Number of Cell at Indexpath");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
// Audio *cellAudio = (Audio *) [audioFilesArray objectAtIndex:indexPath.row];
Audio *cellAudio = (Audio *) [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell textLabel].text = [cellAudio object].label;
[cell textLabel].font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
[cell textLabel].textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
[cell detailTextLabel].text = [NSString stringWithFormat:NSLocalizedString(@"Credit: %@",nil),cellAudio.credit];
[cell detailTextLabel].font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
//[cell detailTextLabel].text = cellAudio.credit;
NSLog(@"cell credit:%@", cellAudio.credit);
[cell setBackgroundColor:[UIColor colorWithRed:.8 green:1 blue:1 alpha:1]];
cell.imageView.image = inactiveTrack.image;
cell.imageView.highlightedImage = inactiveTrack.highlightedImage;
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//NSArray *speciInSection = [sectionsArray objectAtIndex:indexPath.section];
// Configure the cell with the time zone's name.
Speci *tmpSpeci = (Speci *)[(Audio *)[self.fetchedResultsController objectAtIndexPath:indexPath] object];
//Handler has been built to deal with being used in iPad or iPhone. Currently only used on iPhone.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_rightViewReference.detailSpeci = tmpSpeci;
} else{
//New iPhone View
iPhoneDetailViewController *detailViewController = [[iPhoneDetailViewController alloc] initWithNibName:@"iPhoneDetailViewController" bundle:nil];
detailViewController.detailSpeci = tmpSpeci;
detailViewController.title = tmpSpeci.label;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Play Audio if Row Selected
//28th Nov One Audio player for view.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = activeTrack.image;
cell.imageView.highlightedImage = activeTrack.highlightedImage;
cell.imageView.animationImages = activeTrack.animationImages;
cell.imageView.highlightedAnimationImages = activeTrack.highlightedAnimationImages;
cell.imageView.animationDuration = 1;
[cell.imageView startAnimating];
NSLog(@"AnimationCellCount:%d", [cell.imageView.animationImages count]);
//Audio *selectedAudio = [audioFilesArray objectAtIndex:indexPath.row];
Audio *selectedAudio = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"selectedAudio:%@", selectedAudio.filename);
[self.fileRowLocations setObject:indexPath forKey:selectedAudio.filename];
NSLog(@"After fileRowLocations");
[self playAudio:selectedAudio];
}
#pragma mark -
#pragma mark AudioHandling
- (BOOL)playAudio:(Audio *) selectedAudio
{
NSError *error;
NSString *tmpAudioLocation;
//Need to stop current audio being played multiple times
tmpAudioLocation = [[NSBundle mainBundle] pathForResource:[[selectedAudio filename] stringByDeletingPathExtension] ofType:@"mp3"];
if (![[NSFileManager defaultManager] fileExistsAtPath:tmpAudioLocation]) return NO;
NSLog(@"Audio location %@", tmpAudioLocation);
if (self.player !=nil) {
//Exisiting player in place, set delegate to nil
self.player.delegate = nil;
}
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:tmpAudioLocation] error:&error];
if (!self.player)
{
NSLog(@"Error: %@", [error localizedDescription]);
return NO;
}
self.player.delegate = self;
[self.player prepareToPlay];
[self.player play];
NSLog(@"After player prepare");
return YES;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)passedplayer successfully:(BOOL)flag
{
NSLog(@"Player Finished Playing");
NSURL *tmpURL = passedplayer.url;
NSLog(@"Player URL:%@",[tmpURL lastPathComponent]);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:(NSIndexPath *)[self.fileRowLocations objectForKey:[tmpURL lastPathComponent]]];
[cell.imageView stopAnimating];
cell.imageView.image = inactiveTrack.image;
cell.imageView.highlightedImage = inactiveTrack.highlightedImage;
[cell setNeedsDisplay];
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Audio" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"filename" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
//Set up predicate
// NSString *searchTerm = [NSString stringWithFormat:@"group.label ='%@'",__selectedGroup.label];
// NSLog(@"Seleced Group %@", __selectedGroup.label);
// NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:searchTerm];
// [fetchRequest setPredicate:searchPredicate];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"audiofiles"] autorelease];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
player.delegate = nil;
NSLog(@"View did unload");
}
- (void)dealloc {
NSLog(@"AudioListDealloc");
player.delegate = nil;
[fileRowLocations release];
[activeTrack release];
[inactiveTrack release];
[audioFilesArray release];
[player release];
[super dealloc];
}
@end