-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLecturePlayerViewController.m
254 lines (207 loc) · 6.89 KB
/
LecturePlayerViewController.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
//
// LecturePlayerViewController.m
// LectureLeaks
//
// Created by Christopher Ballinger on 6/9/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "LecturePlayerViewController.h"
#import "ASIFormDataRequest.h"
@implementation LecturePlayerViewController
@synthesize submitLabel;
@synthesize durationLabel;
@synthesize currentTimeLabel;
@synthesize locationSwitch;
@synthesize progressView;
@synthesize privateDescriptionTextField;
@synthesize publicDescriptionTextField;
@synthesize nameTextField;
@synthesize recording;
@synthesize playerUpdateTimer;
@synthesize playerSlider;
@synthesize playButton;
@synthesize stopButton;
@synthesize submitButton;
@synthesize player;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[player release];
[recording release];
[durationLabel release];
[currentTimeLabel release];
[playerUpdateTimer release];
[playerSlider release];
[playButton release];
[stopButton release];
[submitButton release];
[nameTextField release];
[publicDescriptionTextField release];
[privateDescriptionTextField release];
[progressView release];
[submitLabel release];
[locationSwitch release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
nameTextField.text = recording.name;
privateDescriptionTextField.text = recording.privateDescription;
publicDescriptionTextField.text = recording.publicDescription;
NSURL *url = recording.url;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
submitButton.enabled = YES;
isPlaying = NO;
playerUpdateTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(updateElapsedTime:) userInfo:nil repeats:YES] retain];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textureme.png"]];
self.title = [recording.date description];
if(recording.isSubmitted)
{
submitLabel.text = @"Previously Submitted";
submitLabel.textColor = [UIColor greenColor];
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[self stopPressed:nil];
}
-(void)viewDidDisappear:(BOOL)animated
{
[player pause];
[playerUpdateTimer invalidate];
}
- (void)viewDidUnload
{
[self setDurationLabel:nil];
[self setCurrentTimeLabel:nil];
[self setPlayerSlider:nil];
[self setPlayButton:nil];
[self setStopButton:nil];
[self setSubmitButton:nil];
[self setNameTextField:nil];
[self setPublicDescriptionTextField:nil];
[self setPrivateDescriptionTextField:nil];
[self setProgressView:nil];
[self setSubmitLabel:nil];
[self setLocationSwitch:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Update the call timer once a second.
- (void) updateElapsedTime:(NSTimer *) timer
{
int currentTime = player.currentTime;
duration = player.duration;
[self updateLabel:currentTimeLabel withTime:currentTime];
[self updateLabel:durationLabel withTime:duration];
if(duration != 0)
{
self.playerSlider.value = currentTime / ((float)duration);
}
if(!isPlaying)
{
playButton.title = @"Play";
playButton.enabled = YES;
}
}
-(void) updateLabel:(UILabel*)label withTime:(NSTimeInterval)time
{
int hour, minute, second;
hour = time / 3600;
minute = (time - hour * 3600) / 60;
second = (time - hour * 3600 - minute * 60);
label.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second];
}
- (IBAction)seek:(id)sender
{
float currentTime = self.playerSlider.value * duration;
[player setCurrentTime:currentTime];
[self updateElapsedTime:nil];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
if(!locationSwitch.on)
recording.location = @"";
[recording submitRecordingWithDelegate:self];
submitLabel.text = @"Submitting...";
submitLabel.textColor = [UIColor whiteColor];
}
}
- (IBAction)submitPressed:(id)sender
{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Upload to OpenWatch" message:@"Would you like to upload your recording to OpenWatch? If you have enabled location services, you can provide us additional geographical context to the recording to help us identify regional trends. For more information visit openwatch.net/privacy." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:@"Upload"];
[alert show];
}
- (IBAction)playPressed:(id)sender
{
if(!isPlaying)
{
[player play];
playButton.title = @"Pause";
stopButton.enabled = YES;
isPlaying = YES;
}
else
{
[player pause];
playButton.title = @"Play";
isPlaying = NO;
}
}
- (IBAction)stopPressed:(id)sender
{
[player pause];
[player setCurrentTime:0];
[self updateElapsedTime:nil];
stopButton.enabled = NO;
playButton.title = @"Play";
isPlaying = NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
recording.name = nameTextField.text;
recording.privateDescription = privateDescriptionTextField.text;
recording.publicDescription = publicDescriptionTextField.text;
[recording saveMetadata];
return YES;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Complete" message:@"The recording was uploaded successfully to www.openwatch.net" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
// Set TRUE if file was sent properly
recording.isSubmitted = YES;
[recording saveMetadata];
submitLabel.text = @"Submission successful!";
submitLabel.textColor = [UIColor greenColor];
progressView.hidden = TRUE;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Error" message:@"Upload failed, please check your internet connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
submitLabel.text = @"Submission failed!";
submitLabel.textColor = [UIColor redColor];
progressView.hidden = TRUE;
}
@end