-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathReactNativeAudioStreaming.m
More file actions
500 lines (407 loc) · 17.8 KB
/
Copy pathReactNativeAudioStreaming.m
File metadata and controls
500 lines (407 loc) · 17.8 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import "ReactNativeAudioStreaming.h"
#define LPN_AUDIO_BUFFER_SEC 20 // Can't use this with shoutcast buffer meta data
@import AVFoundation;
@import MediaPlayer;
@implementation ReactNativeAudioStreaming
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (ReactNativeAudioStreaming *)init
{
self = [super init];
if (self) {
[self setSharedAudioSessionCategory];
self.audioPlayer = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES }];
[self.audioPlayer setDelegate:self];
self.lastUrlString = @"";
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
NSLog(@"AudioPlayer initialized");
}
return self;
}
-(void) tick:(NSTimer*)timer
{
if (!self.audioPlayer) {
return;
}
if (self.audioPlayer.currentlyPlayingQueueItemId != nil && self.audioPlayer.state == STKAudioPlayerStatePlaying) {
NSNumber *progress = [NSNumber numberWithFloat:self.audioPlayer.progress];
NSNumber *duration = [NSNumber numberWithFloat:self.audioPlayer.duration];
NSString *url = [NSString stringWithString:self.audioPlayer.currentlyPlayingQueueItemId];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent" body:@{
@"status": @"STREAMING",
@"progress": progress,
@"duration": duration,
@"url": url,
}];
}
}
- (void)dealloc
{
[self unregisterAudioInterruptionNotifications];
[self unregisterRemoteControlEvents];
[self.audioPlayer setDelegate:nil];
}
#pragma mark - Pubic API
RCT_EXPORT_METHOD(play:(NSString *) streamUrl options:(NSDictionary *)options)
{
if (!self.audioPlayer) {
return;
}
[self activate];
if (self.audioPlayer.state == STKAudioPlayerStatePaused && [self.lastUrlString isEqualToString:streamUrl]) {
[self.audioPlayer resume];
} else {
[self.audioPlayer play:streamUrl];
}
self.fetchedAlbumArt = false;
self.lastUrlString = streamUrl;
self.showNowPlayingInfo = false;
if ([options objectForKey:@"showIniOSMediaCenter"]) {
if ([options objectForKey:@"metadata"]) {
NSDictionary *metadata = [options objectForKey:@"metadata"];
self.currentSong = [metadata objectForKey:@"title"];
self.currentArtist = [metadata objectForKey:@"artist"];
self.currentArtwork = [metadata objectForKey:@"artwork"];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent" body:@{
@"status": @"METADATA_UPDATED",
@"key": @"StreamTitle",
@"value": self.currentSong
}];
}
self.showNowPlayingInfo = [[options objectForKey:@"showIniOSMediaCenter"] boolValue];
}
if (self.showNowPlayingInfo) {
//unregister any existing registrations
[self unregisterAudioInterruptionNotifications];
[self unregisterRemoteControlEvents];
//register
[self registerAudioInterruptionNotifications];
[self registerRemoteControlEvents];
}
[self setNowPlayingInfo:true];
}
RCT_EXPORT_METHOD(seekToTime:(double) seconds)
{
if (!self.audioPlayer) {
return;
}
[self.audioPlayer seekToTime:seconds];
}
RCT_EXPORT_METHOD(goForward:(double) seconds)
{
if (!self.audioPlayer) {
return;
}
double newtime = self.audioPlayer.progress + seconds;
if (self.audioPlayer.duration < newtime) {
[self.audioPlayer stop];
[self setNowPlayingInfo:false];
} else {
[self.audioPlayer seekToTime:newtime];
}
}
RCT_EXPORT_METHOD(goBack:(double) seconds)
{
if (!self.audioPlayer) {
return;
}
double newtime = self.audioPlayer.progress - seconds;
if (newtime < 0) {
[self.audioPlayer seekToTime:0.0];
} else {
[self.audioPlayer seekToTime:newtime];
}
}
RCT_EXPORT_METHOD(pause)
{
if (!self.audioPlayer) {
return;
} else {
[self.audioPlayer pause];
[self setNowPlayingInfo:false];
[self deactivate];
}
}
RCT_EXPORT_METHOD(resume)
{
if (!self.audioPlayer) {
return;
} else {
[self activate];
[self.audioPlayer resume];
[self setNowPlayingInfo:true];
}
}
RCT_EXPORT_METHOD(stop)
{
if (!self.audioPlayer) {
return;
} else {
[self.audioPlayer stop];
[self setNowPlayingInfo:false];
[self deactivate];
}
}
RCT_EXPORT_METHOD(getStatus: (RCTResponseSenderBlock) callback)
{
NSString *status = @"STOPPED";
NSNumber *duration = [NSNumber numberWithFloat:self.audioPlayer.duration];
NSNumber *progress = [NSNumber numberWithFloat:self.audioPlayer.progress];
if (!self.audioPlayer) {
status = @"ERROR";
} else if ([self.audioPlayer state] == STKAudioPlayerStatePlaying) {
status = @"PLAYING";
} else if ([self.audioPlayer state] == STKAudioPlayerStatePaused) {
status = @"PAUSED";
} else if ([self.audioPlayer state] == STKAudioPlayerStateBuffering) {
status = @"BUFFERING";
}
callback(@[[NSNull null], @{@"status": status, @"progress": progress, @"duration": duration, @"url": self.lastUrlString}]);
}
#pragma mark - StreamingKit Audio Player
- (void)audioPlayer:(STKAudioPlayer *)player didStartPlayingQueueItemId:(NSObject *)queueItemId
{
NSLog(@"AudioPlayer is playing");
}
- (void)audioPlayer:(STKAudioPlayer *)player didFinishPlayingQueueItemId:(NSObject *)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration
{
NSLog(@"AudioPlayer has stopped");
}
- (void)audioPlayer:(STKAudioPlayer *)player didFinishBufferingSourceWithQueueItemId:(NSObject *)queueItemId
{
NSLog(@"AudioPlayer finished buffering");
}
- (void)audioPlayer:(STKAudioPlayer *)player unexpectedError:(STKAudioPlayerErrorCode)errorCode {
NSLog(@"AudioPlayer unexpected Error with code %d", errorCode);
}
- (void)audioPlayer:(STKAudioPlayer *)audioPlayer didReadStreamMetadata:(NSDictionary *)dictionary {
NSLog(@"AudioPlayer SONG NAME %@", dictionary[@"StreamTitle"]);
self.currentSong = dictionary[@"StreamTitle"] ? dictionary[@"StreamTitle"] : @"";
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent" body:@{
@"status": @"METADATA_UPDATED",
@"key": @"StreamTitle",
@"value": self.currentSong
}];
[self setNowPlayingInfo:true];
}
- (void)audioPlayer:(STKAudioPlayer *)player stateChanged:(STKAudioPlayerState)state previousState:(STKAudioPlayerState)previousState
{
NSNumber *duration = [NSNumber numberWithFloat:self.audioPlayer.duration];
NSNumber *progress = [NSNumber numberWithFloat:self.audioPlayer.progress];
switch (state) {
case STKAudioPlayerStatePlaying:
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent"
body:@{@"status": @"PLAYING", @"progress": progress, @"duration": duration, @"url": self.lastUrlString}];
break;
case STKAudioPlayerStatePaused:
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent"
body:@{@"status": @"PAUSED", @"progress": progress, @"duration": duration, @"url": self.lastUrlString}];
break;
case STKAudioPlayerStateStopped:
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent"
body:@{@"status": @"STOPPED", @"progress": progress, @"duration": duration, @"url": self.lastUrlString}];
break;
case STKAudioPlayerStateBuffering:
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent"
body:@{@"status": @"BUFFERING"}];
break;
case STKAudioPlayerStateError:
[self.bridge.eventDispatcher sendDeviceEventWithName:@"AudioBridgeEvent"
body:@{@"status": @"ERROR"}];
break;
default:
break;
}
}
#pragma mark - Audio Session
- (void)activate
{
NSError *categoryError = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&categoryError];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&categoryError];
if (categoryError) {
NSLog(@"Error setting category! %@", [categoryError description]);
}
}
- (void)deactivate
{
NSError *categoryError = nil;
[[AVAudioSession sharedInstance] setActive:NO error:&categoryError];
if (categoryError) {
NSLog(@"Error setting category! %@", [categoryError description]);
}
}
- (void)setSharedAudioSessionCategory
{
NSError *categoryError = nil;
self.isPlayingWithOthers = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
[[AVAudioSession sharedInstance] setActive:NO error:&categoryError];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&categoryError];
if (categoryError) {
NSLog(@"Error setting category! %@", [categoryError description]);
}
}
- (void)registerAudioInterruptionNotifications
{
// Register for audio interrupt notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onAudioInterruption:)
name:AVAudioSessionInterruptionNotification
object:nil];
// Register for route change notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onRouteChangeInterruption:)
name:AVAudioSessionRouteChangeNotification
object:nil];
}
- (void)unregisterAudioInterruptionNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVAudioSessionRouteChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVAudioSessionInterruptionNotification
object:nil];
}
- (void)onAudioInterruption:(NSNotification *)notification
{
// Get the user info dictionary
NSDictionary *interruptionDict = notification.userInfo;
// Get the AVAudioSessionInterruptionTypeKey enum from the dictionary
NSInteger interuptionType = [[interruptionDict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
// Decide what to do based on interruption type
switch (interuptionType)
{
case AVAudioSessionInterruptionTypeBegan:
NSLog(@"Audio Session Interruption case started.");
[self.audioPlayer pause];
break;
case AVAudioSessionInterruptionTypeEnded:
NSLog(@"Audio Session Interruption case ended.");
self.isPlayingWithOthers = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
(self.isPlayingWithOthers) ? [self.audioPlayer stop] : [self.audioPlayer resume];
break;
default:
NSLog(@"Audio Session Interruption Notification case default.");
break;
}
}
- (void)onRouteChangeInterruption:(NSNotification *)notification
{
NSDictionary *interruptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interruptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (routeChangeReason)
{
case AVAudioSessionRouteChangeReasonUnknown:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonUnknown");
break;
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
// A user action (such as plugging in a headset) has made a preferred audio route available.
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonNewDeviceAvailable");
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
// The previous audio output path is no longer available.
[self.audioPlayer stop];
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
// The category of the session object changed. Also used when the session is first activated.
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonCategoryChange"); //AVAudioSessionRouteChangeReasonCategoryChange
break;
case AVAudioSessionRouteChangeReasonOverride:
// The output route was overridden by the app.
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonOverride");
break;
case AVAudioSessionRouteChangeReasonWakeFromSleep:
// The route changed when the device woke up from sleep.
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonWakeFromSleep");
break;
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
// The route changed because no suitable route is now available for the specified category.
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory");
break;
}
}
#pragma mark - Remote Control Events
- (void)registerRemoteControlEvents
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTarget:self action:@selector(didReceivePlayCommand:)];
[commandCenter.pauseCommand addTarget:self action:@selector(didReceivePauseCommand:)];
commandCenter.playCommand.enabled = YES;
commandCenter.pauseCommand.enabled = YES;
commandCenter.stopCommand.enabled = NO;
commandCenter.nextTrackCommand.enabled = NO;
commandCenter.previousTrackCommand.enabled = NO;
}
- (MPRemoteCommandHandlerStatus)didReceivePlayCommand:(MPRemoteCommand *)event
{
NSLog(@"didReceivePlayCommand");
[self resume];
return MPRemoteCommandHandlerStatusSuccess;
}
- (MPRemoteCommandHandlerStatus)didReceivePauseCommand:(MPRemoteCommand *)event
{
NSLog(@"didReceivePauseCommand");
[self pause];
return MPRemoteCommandHandlerStatusSuccess;
}
- (void)unregisterRemoteControlEvents
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand removeTarget:self];
[commandCenter.pauseCommand removeTarget:self];
}
+ (void) loadFromURL: (NSURL*) url callback:(void (^)(UIImage *image))callback {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
callback(image);
});
});
}
- (void)setNowPlayingInfo:(bool)isPlaying
{
if (self.showNowPlayingInfo) {
NSString* bundleDisplayName =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString* appName = bundleDisplayName? bundleDisplayName :
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
NSString* title = self.currentSong ? self.currentSong : @"";
NSString* artist = self.currentArtist ? self.currentArtist : @"";
NSString* applicationName = appName ? appName : @"AppName";
if (!self.fetchedAlbumArt && self.currentArtwork != nil) {
NSString* imageUrl = self.currentArtwork;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
UIImage *artworkImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
if(artworkImage) {
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: artworkImage];
NSMutableDictionary *songInfo =
infoCenter.nowPlayingInfo ? [infoCenter.nowPlayingInfo mutableCopy] : [NSMutableDictionary dictionary];
[songInfo setValue:albumArt forKey:MPMediaItemPropertyArtwork];
infoCenter.nowPlayingInfo = songInfo;
}
self.fetchedAlbumArt = true;
});
}
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *nowPlayingInfo = infoCenter.nowPlayingInfo ?
[NSMutableDictionary dictionaryWithDictionary:infoCenter.nowPlayingInfo ] : [NSMutableDictionary dictionary];
[nowPlayingInfo setValue:title forKey:MPMediaItemPropertyAlbumTitle];
[nowPlayingInfo setValue:artist forKey:MPMediaItemPropertyArtist];
[nowPlayingInfo setValue:applicationName forKey:MPMediaItemPropertyTitle];
[nowPlayingInfo setValue:[NSNumber numberWithFloat:isPlaying ? 1.0f : 0.0] forKey:MPNowPlayingInfoPropertyPlaybackRate];
infoCenter.nowPlayingInfo = nowPlayingInfo;
} else {
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
}
}
@end