-
Notifications
You must be signed in to change notification settings - Fork 2
/
SRPlayer.m
352 lines (290 loc) · 10.7 KB
/
SRPlayer.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
//
// SRPlayer.m
// Soundrocket
//
// Created by Sebastian Boldt on 01.06.15.
// Copyright (c) 2015 sebastianboldt. All rights reserved.
//
// Libarys
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <SVProgressHUD.h>
// Own classes
#import "SRPlayer.h"
#import "Track.h"
#import "SRStore.h"
#import "SRAuthenticator.h"
#import "SRHelper.h"
#import "SRRequestModel.h"
#import "Soundrocket-SWIFT.h"
@interface SRPlayer()
@property (nonatomic,strong) AVAsset * currentAsset;
@property (nonatomic,strong) AVPlayerItem * streamingItem;
@property (nonatomic) BOOL loopCurrentTrack;
@property (nonatomic) BOOL playNextTrackAfterLoad;
@property (nonatomic) NSUInteger lastTrackIndex;
@property (nonatomic,strong) NSMutableArray * delegates;
@property (nonatomic,strong) AVPlayer * soundPlayer;
@end
@interface SRPlayer() <SRRequestModelDelegate>
@end
#pragma mark - Implementation
@implementation SRPlayer
+ (instancetype)sharedPlayer {
static SRPlayer *_sharedPlayer = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Init Stuff
_sharedPlayer = [[SRPlayer alloc]init];
_sharedPlayer.soundPlayer = [[AVPlayer alloc]init];
[_sharedPlayer.soundPlayer addObserver:_sharedPlayer forKeyPath:@"rate" options:0 context:nil];
_sharedPlayer.delegates = [[NSMutableArray alloc]init];
_sharedPlayer.upNext = [[NSMutableArray alloc]init];
_sharedPlayer.history = [[NSMutableArray alloc]init];
_sharedPlayer.playingIndex = 0;
});
return _sharedPlayer;
}
-(void)addDelegate:(id<SRPlayerDelegate>)delegate{
if (![self.delegates containsObject:delegate]) {
[self.delegates addObject:delegate];
}
}
-(void)removeDelegate:(id)delegate{
if ([self.delegates containsObject:delegate]) {
[self.delegates removeObject:delegate];
}
}
-(void)setModel:(SRRequestModel *)model {
[_model removeDelegate:self];
_model = model;
[model addDelegate:self];
}
-(void)setCurrentTrack:(Track *)currentTrack {
// Remove ourself as an Observer
@try {
[self.streamingItem removeObserver:self forKeyPath:@"playbackBufferEmpty" ];
[self.streamingItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
[[NSNotificationCenter defaultCenter]removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.streamingItem];
}
@catch (NSException *exception) {
}
_currentTrack = currentTrack;
// Inform all delegates
for (id<SRPlayerDelegate> delegate in self.delegates) {
[delegate player:self willStartWithTrack:currentTrack fromIndexPath:self.playingIndex];
}
// Do setup stuff
NSString *streamURL = nil;
NSString *urlString = nil;
NSString *client_ID = nil;
if ([self loadStreamingID]) {
client_ID = [self loadStreamingID];
} else {
client_ID = @"3ceea65b3d83ab630bc818ce1d179a82";
}
NSURL *url = nil;
if(currentTrack.local_path){
streamURL =currentTrack.local_path;
url = [[NSURL alloc]initFileURLWithPath:currentTrack.local_path];
for (id<SRPlayerDelegate> delegate in self.delegates) {
[delegate player:self willStartWithOfflineTrack:self.currentTrack];
}
} else {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSString * token = @"";
if ([SRAuthenticator sharedAuthenticator].authToken) {
token =[SRAuthenticator sharedAuthenticator].authToken;
} else {
token = [defaults objectForKey:@"access_token"];
}
streamURL = currentTrack.stream_url;
urlString = [NSString stringWithFormat:@"%@?client_id=%@&oauth_token=%@", streamURL, client_ID,token];
url = [NSURL URLWithString:urlString];
}
self.currentAsset = [AVURLAsset URLAssetWithURL:url options:nil];
self.streamingItem = [AVPlayerItem playerItemWithAsset:self.currentAsset];
[self.streamingItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[self.streamingItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.streamingItem];
[self.soundPlayer replaceCurrentItemWithPlayerItem:self.streamingItem];
if (true) {
[SRStore storeStrackToHistory:currentTrack];
}
[self.soundPlayer play];
}
-(void)itemDidFinishPlaying:(id)sender {
NSLog(@"Called");
for (id<SRPlayerDelegate> delegate in self.delegates) {
[delegate player:self didFinishWithTrack:self.currentTrack];
}
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"repeatStatus"]) {
BOOL isActive = [[defaults objectForKey:@"repeatStatus"]boolValue];
if (isActive) {
[self seekToTime:CMTimeMake(0,1) completionHandler:nil];
[self play];
} else {
[self playNextTrack];
}
} else {
[self playNextTrack];
}
}
-(void)play {
[self.soundPlayer play];
}
-(void)pause {
[self.soundPlayer pause];
}
-(void)stop {
self.streamingItem = nil;
}
-(float)rate {
return self.soundPlayer.rate;
}
-(CMTime)currentTime{
return self.soundPlayer.currentItem.currentTime;
}
-(void)seekToTime:(CMTime)time completionHandler:(void (^)(BOOL))completionHandler {
[self.soundPlayer seekToTime:time completionHandler:^(BOOL finished){
if (finished)
{
if(completionHandler){
completionHandler(true);
}
}
}];
}
-(void)playNextTrack {
NSIndexPath * index = self.playingIndex;
if (self.model) {
if ((index.row+1)< [self.model.justTracksAndReposts count]) {
[self setPlayingIndex:[NSIndexPath indexPathForItem:index.row+1 inSection:1]];
[self setCurrentTrack:[self.model.justTracksAndReposts objectAtIndex:index.row +1]];
} else {
self.playNextTrackAfterLoad = YES;
[self.model load];
}
} else {
NSMutableArray * currentTracks = self.upNext;
if ((index.row+1)< [currentTracks count]) {
[self setPlayingIndex:[NSIndexPath indexPathForItem:index.row+1 inSection:1]];
[self setCurrentTrack:[currentTracks objectAtIndex:index.row +1]];
}
}
}
-(void)playLastTrack {
NSIndexPath * index = self.playingIndex;
[self setPlayingIndex:[NSIndexPath indexPathForItem:index.row+-1 inSection:1]];
if (self.model) {
NSMutableArray * currentTracks = self.model.justTracksAndReposts;
if ((index.row-1)>= 0) {
[self setCurrentTrack:[currentTracks objectAtIndex:index.row -1]];
}
} else {
NSMutableArray * currentTracks = self.upNext;
if ((index.row-1)>= 0) {
[self setCurrentTrack:[currentTracks objectAtIndex:index.row -1]];
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// Wenn rate vorhanden ist dann wir dgepsielt wenn nicht ist pause
if ([keyPath isEqualToString:@"rate"]) {
if ([self.soundPlayer rate]) {
for (id<SRPlayerDelegate> delegate in self.delegates) {
[delegate player:self willPlayTrack:self.currentTrack];
}
}
else {
for (id<SRPlayerDelegate> delegate in self.delegates) {
[delegate player:self willPauseTrack:self.currentTrack];
}
}
}
else if (object == self.streamingItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
if (self.streamingItem.playbackBufferEmpty) {
[self pause];
}
}
else if (object == self.streamingItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (self.streamingItem.playbackLikelyToKeepUp)
{
// Buffer ready
}
}
else if (object == self.soundPlayer && [keyPath isEqualToString:@"status"]) {
if (self.soundPlayer.status == AVPlayerStatusReadyToPlay) {
for (id<SRPlayerDelegate> delegate in self.delegates) {
[delegate player:self isReadyToPlayTrack:self.currentTrack];
}
} else if (self.soundPlayer.status == AVPlayerStatusFailed) {
}
}
}
-(NSTimeInterval)availableDuration {
NSArray * loadedTimeRanges = [[self.soundPlayer currentItem]loadedTimeRanges];
if ([loadedTimeRanges count] != 0 ) {
CMTimeRange timeRange = [[loadedTimeRanges objectAtIndex:0]CMTimeRangeValue];
Float64 startSeconds = CMTimeGetSeconds(timeRange.start);
Float64 durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval result = startSeconds + durationSeconds;
return result;
}
return 0;
}
-(AVPlayerItemStatus)streamingStatus {
return self.soundPlayer.currentItem.status;
}
-(CMTime)durationOfCurrentItem {
return [[[self.soundPlayer currentItem] asset] duration];
}
-(void)togglePlayback {
if ([self.soundPlayer rate] != 0.0) {
[self pause];
} else [self play];
}
#pragma mark - SRRequestModelDelegate
-(void)requestModelDidFailWithLoading:(SRRequestModel *)requestModel withError:(NSError *)error {
[SVProgressHUD showErrorWithStatus:error.localizedDescription];
}
-(void)requestModelDidFinishLoading:(SRRequestModel *)requestModel {
[SVProgressHUD dismiss];
if (self.playNextTrackAfterLoad) {
[self playNextTrack];
}
self.playNextTrackAfterLoad = NO;
}
-(void)requestModelDidStartLoading:(SRRequestModel *)requestModel {
}
-(NSMutableArray *)upNext {
if ([_upNext count]>0 && (self.model == nil)) {
return _upNext;
} else {
return self.model.results;
}
}
-(void)saveStreamingID:(NSString*)string {
NSUserDefaults * sharedDefaults = [NSUserDefaults standardUserDefaults];
if ([string isEqualToString:@""] || string == nil || string == [NSNull class]) {
[sharedDefaults removeObjectForKey:@"CLIENT_ID"];
} else {
[sharedDefaults setObject:string forKey:@"CLIENT_ID"];
}
[sharedDefaults synchronize];
}
-(NSString*)loadStreamingID {
NSUserDefaults * sharedDefaults = [NSUserDefaults standardUserDefaults];
return [sharedDefaults objectForKey:@"CLIENT_ID"];
}
-(BOOL)isPlaying {
if([self rate] == 0.0){
return NO;
} else {
return YES;
}
}
@end