-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerViewController.m
More file actions
255 lines (163 loc) · 8.45 KB
/
Copy pathPlayerViewController.m
File metadata and controls
255 lines (163 loc) · 8.45 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// PlayerViewController.m
// AudioVisualRecorder
//
// Created by Nicole Lehrer on 12/22/14.
// Copyright (c) 2014 Nicole Lehrer. All rights reserved.
//
#import "PlayerView.h"
#import "PlayerViewController.h"
#import "Constants.h"
static const NSString *ItemStatusContext;
@interface PlayerViewController ()
@property (nonatomic) float assetLength;
@property (nonatomic) float playBackCounter;
@property (nonatomic) NSTimer * audioSampleTimer;
@end
@implementation PlayerViewController
@synthesize player = _player;
@synthesize playerItem = _playerItem;
@synthesize playerView = _playerView;
@synthesize fileURL = _fileURL;
@synthesize audioLevelSummary = _audioLevelSummary;
@synthesize playBackCounter = _playBackCounter;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self syncUI];
// self.playerView = [[PlayerView alloc] initWithFrame:CGRectMake(100, 100, 200,200)];
// self.playerView = [[PlayerView alloc] init];
self.playerView.backgroundColor = [UIColor blackColor];
// [self.view addSubview:self.playerView];
// [[NSNotificationCenter defaultCenter]
// addObserver:self
// selector:@selector(playerItemDidReachEnd:)
// name:AVPlayerItemDidPlayToEndTimeNotification
// object:[self.player currentItem]];
[self loadAssetFromFile];
// i think it's important to display the full stream before playback because visually it allows the patient to look at the biggest drop or peak in the audio levels and focus their attention in advance of hearing the audio that caused it. then when the audio plays back, you should provide a real-time updater (each audio level segment corresponds to .25 second sample, so draw a another highlight bar when that segment is being played) so that the patient can then be ready to hear to word(s) that caused this drop in volume or peak.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)syncUI {
if ((self.player.currentItem != nil) &&
([self.player.currentItem status] == AVPlayerItemStatusReadyToPlay)) {
self.playButton.enabled = YES;
}
else {
self.playButton.enabled = NO;
}
}
- (void)loadAssetFromFile {
//add if we are observing then remove observer to prevent errors.
// NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mov"];
if(self.fileURL){
NSLog(@"valid url is %@", self.fileURL);
}
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
^{
// The completion block goes here.
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
// ensure that this is done before the playerItem is associated with the player
[self.playerItem addObserver:self forKeyPath:@"status"
options:NSKeyValueObservingOptionInitial context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerItem];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
[self.playerView setPlayer:self.player];
}
else {
// You should deal with the error appropriately.
NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
}
});
}];
self.assetLength = CMTimeGetSeconds(asset.duration);
NSLog(@"length of asset: %f sec", self.assetLength);
[self playBackDrawing];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.player pause];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
if (self.player != nil && [self.player currentItem] != nil){
[self.playerItem removeObserver:self forKeyPath:@"status"];
}
[self.audioSampleTimer invalidate];
});
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (context == &ItemStatusContext) {
dispatch_async(dispatch_get_main_queue(),
^{
[self syncUI];
});
return;
}
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
return;
}
- (IBAction)play:sender {
[self.player play];
[self startAnimation];
}
-(void)startAnimation
{
self.audioSampleTimer = [NSTimer scheduledTimerWithTimeInterval:SAMPLE_FREQ target:self selector:@selector(realTimeplayBackDrawing) userInfo:nil repeats:YES];
}
-(void)realTimeplayBackDrawing
{
if (CMTimeGetSeconds(self.playerItem.currentTime) < self.assetLength && CMTimeGetSeconds(self.playerItem.currentTime) > 0) {
NSLog(@"comparing this current time %f to total time %f", CMTimeGetSeconds(self.playerItem.currentTime), self.assetLength);
float viewWidth = self.view.frame.size.width;
float viewSamplePerSoundSample = viewWidth/[self.audioLevelSummary count];
int spacer = 1;
int baseHeight = self.view.frame.size.height-100;
float barHeight = 90+[[self.audioLevelSummary objectAtIndex:self.playBackCounter] floatValue]; //-90 is the max decibel level on meter
UIView * block = [[UIView alloc] initWithFrame:CGRectMake(self.playBackCounter*(spacer+viewSamplePerSoundSample), baseHeight-barHeight/2, viewSamplePerSoundSample-spacer, barHeight)];
block.backgroundColor = [UIColor blueColor];
[self.view addSubview:block];
self.playBackCounter++;
}
}
-(void)playBackDrawing
{
float viewWidth = self.view.frame.size.width;
NSLog(@"viewWidth is %f", viewWidth);
float viewSamplePerSoundSample = viewWidth/[self.audioLevelSummary count];
int i;
for (i = 0; i<[self.audioLevelSummary count]; i++) {
int spacer = 1;
int baseHeight = self.view.frame.size.height-100;
float barHeight = 90+[[self.audioLevelSummary objectAtIndex:i] floatValue]; //-90 is the max decibel level on meter
UIView * block = [[UIView alloc] initWithFrame:CGRectMake(i*(spacer+viewSamplePerSoundSample), baseHeight-barHeight/2, viewSamplePerSoundSample-spacer, barHeight)];
block.backgroundColor = [UIColor blackColor];
[self.view addSubview:block];
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[self.player seekToTime:kCMTimeZero];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end