-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathULMusicPlayerController.m
341 lines (292 loc) · 10.6 KB
/
ULMusicPlayerController.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
//
// MusicPlayerController.m
// Fuge
//
// Created by Mikhail Larionov on 10/8/13.
//
//
#import "ULMusicPlayerController.h"
#import "ULDeezerWrapper.h"
//////////////////////////////////////////////////////
// Reusable play button
//////////////////////////////////////////////////////
@implementation ULMusicPlayButton
+(ULMusicPlayButton*) buttonWithArtist:(NSString*)artist
{
ULMusicPlayButton* result = [ULMusicPlayButton buttonWithType:UIButtonTypeCustom];
result.frame = CGRectMake(0, 0, 50, 50);
[result updateArtistInfo:artist];
return result;
}
-(void) updateArtistInfo:(NSString*)artist
{
// Create indicator
if ( ! _activityIndicator )
{
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicator.frame = CGRectMake(15, 15, 20, 20);
_activityIndicator.color = [UIColor whiteColor];
[self addSubview:_activityIndicator];
}
// Play icon
if ( ! _iconView )
{
_iconView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 16, 18, 18)];
[self addSubview:_iconView];
}
// Rating image
if ( ! _ratingView )
{
_ratingView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ratingBar"]];
_ratingView.frame = CGRectMake(7, 50, 36, 5);
_ratingView.contentMode = UIViewContentModeTopLeft;
_ratingView.clipsToBounds = YES;
[self addSubview:_ratingView];
_ratingView.hidden = YES;
}
// Misc preparations
[_activityIndicator startAnimating];
_artistName = artist;
// Observer for playback
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(artistChanged)
name:kMusicArtistChanged object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(artistChanged)
name:kMusicTrackStopped object:nil];
[self addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
// Start loading
[deezerWrapper prepareArtist:_artistName target:self selector:@selector(artistInfoLoadedCallback:)];
}
-(void) artistInfoLoadedCallback:(NSDictionary*)data
{
[_activityIndicator stopAnimating];
// Loading data
[self artistChanged];
// Rating
if ( data )
{
NSArray* ratings = [data objectForKey:@"trackRatings"];
if ( ratings && ratings.count > 0 )
{
// Normalize
NSNumber* rating = ratings[0];
double normalizedRating = [rating doubleValue];
normalizedRating = normalizedRating/1000000.0f;
if ( normalizedRating > 1.0 )
normalizedRating = 1.0;
if ( normalizedRating < 0.1 )
normalizedRating = 0.1;
// Show
if ( _ratingView.hidden )
{
_ratingView.hidden = NO;
double oldWidth = _ratingView.width;
_ratingView.width = 0.0;
[UIView animateWithDuration:0.2 animations:^{
_ratingView.width = normalizedRating * oldWidth;
}];
}
}
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void) artistChanged
{
if ( _activityIndicator.isAnimating )
return;
if ( [deezerWrapper artistInformation:_artistName] )
{
BOOL nowPlaying = [[deezerWrapper nowPlaying] isEqualToString:_artistName];
if ( nowPlaying )
_iconView.image = [UIImage imageNamed:@"iconStop.png"];
else
_iconView.image = [UIImage imageNamed:@"iconPlay.png"];
_iconView.alpha = 1.0;
}
else
{
_iconView.image = [UIImage imageNamed:@"iconNoMusic.png"];
_iconView.alpha = 0.5;
_ratingView.hidden = YES;
}
}
- (void)buttonTapped
{
if ( _artistName )
{
// Start/stop playing
if ( [deezerWrapper artistInformation:_artistName] )
{
if ( [[deezerWrapper nowPlaying] isEqualToString:_artistName] )
[deezerWrapper stopPlaying];
else
{
[deezerWrapper checkVolume];
[deezerWrapper playNextTrack:_artistName inCycle:TRUE];
}
[self artistChanged];
}
}
}
@end
#pragma mark -
//////////////////////////////////////////////////////
// Music panel controller
//////////////////////////////////////////////////////
@implementation ULMusicPlaybackView
-(void) drawRect: (CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:TABLE_FOOTER_COLOR].CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 1);
CGContextAddLineToPoint(context, rect.size.width * deezerWrapper.bufferProgress, 1);
CGContextDrawPath(context, kCGPathStroke);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:TABLE_SEPARATOR_COLOR].CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 1);
CGContextAddLineToPoint(context, rect.size.width * deezerWrapper.trackProgress, 1);
CGContextDrawPath(context, kCGPathStroke);
UIGraphicsEndImageContext();
}
@end
#pragma mark -
//////////////////////////////////////////////////////
// Playback progress
//////////////////////////////////////////////////////
@implementation ULMusicPlayerController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(updatePresentation:)
name:kMusicArtistChanged
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(updatePresentation:)
name:kMusicTrackChanged
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(updatePresentation:)
name:kMusicTrackStopped
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(updateProgress)
name:kTrackOrBufferProgressUpdated
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void) loadBandCover
{
// Create cover object if needed
if ( ! _bandCover )
{
_bandCover = [[AsyncImageView alloc] initWithFrame:CGRectMake(0, 2, 48, 48)];
[self.view addSubview:_bandCover];
[self.view bringSubviewToFront:_activityIndicator];
}
_bandCover.imageView.image = nil;
// Load cover url
NSString* strURL = [deezerWrapper artistCover:deezerWrapper.currentArtist];
if ( ! strURL )
return;
// Start loading
[_activityIndicator startAnimating];
[_bandCover loadImageFromURL:strURL withTarger:_activityIndicator selector:@selector(stopAnimating)];
}
- (void) updatePresentation:(BOOL)animate
{
// Bring to front in case view was hidden behind others
[_parentController.view bringSubviewToFront:self.view];
// Show animation
if ( deezerWrapper.nowPlaying && ! _playbackActive )
{
_playbackActive = YES;
self.view.hidden = NO;
if ( animate )
{
self.view.originY += self.view.height;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.view.originY -= self.view.height;
} completion:nil];
}
}
// Hide animation
if ( ! deezerWrapper.nowPlaying && _playbackActive )
{
_playbackActive = NO;
if ( animate )
{
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.originY += self.view.height;
} completion:^(BOOL finished) {
self.view.originY -= self.view.height;
self.view.hidden = YES;
}];
}
else
self.view.hidden = YES;
}
// Update labels and cover
if ( deezerWrapper.nowPlaying )
{
_artistLabel.text = deezerWrapper.currentArtist;
_trackLabel.text = deezerWrapper.currentTrackName;
_trackNumber.text = [NSString stringWithFormat:@"Track %d of %d", deezerWrapper.currentTrackNumber+1, deezerWrapper.totalTracksCount];
[self loadBandCover];
}
}
- (void)updateProgress
{
// Bring to front in case view was hidden behind others
[_parentController.view bringSubviewToFront:self.view];
// To call redraw method
[_playbackView setNeedsDisplay];
}
- (void)viewDidLoad
{
[super viewDidLoad];
_playbackView = [[ULMusicPlaybackView alloc] initWithFrame:self.view.frame];
_playbackView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
_playbackView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:_playbackView];
[self.view sendSubviewToBack:_playbackView];
[self updatePresentation:NO];
self.view.hidden = TRUE;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self updatePresentation:NO];
}
- (IBAction)nextButtonTapped:(id)sender {
[deezerWrapper playNextTrack:nil inCycle:TRUE];
}
- (IBAction)stopButtonTapped:(id)sender {
[deezerWrapper stopPlaying];
}
- (void) setParentController:(UIViewController*)parent
{
_parentController = parent;
}
+ (ULMusicPlayerController*) createAndAttachToParent:(UIViewController*)parent
{
ULMusicPlayerController* controller = [[ULMusicPlayerController alloc] init];
[controller setParentController:parent];
controller.view.frame = CGRectMake(0, parent.view.height - controller.view.height, parent.view.width, controller.view.height);
[parent.view addSubview:controller.view];
[parent addChildViewController:controller];
return controller;
}
@end