-
Notifications
You must be signed in to change notification settings - Fork 764
/
CDVSound.m
1086 lines (938 loc) · 43.2 KB
/
CDVSound.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "CDVSound.h"
#import "CDVFile.h"
#import <AVFoundation/AVFoundation.h>
#include <math.h>
#define DOCUMENTS_SCHEME_PREFIX @"documents://"
#define HTTP_SCHEME_PREFIX @"http://"
#define HTTPS_SCHEME_PREFIX @"https://"
#define CDVFILE_PREFIX @"cdvfile://"
#define FILE_PREFIX @"file://"
@implementation CDVSound
BOOL keepAvAudioSessionAlwaysActive = NO;
@synthesize soundCache, avSession, currMediaId, statusCallbackId;
-(void) pluginInitialize
{
NSDictionary* settings = self.commandDelegate.settings;
keepAvAudioSessionAlwaysActive = [[settings objectForKey:[@"KeepAVAudioSessionAlwaysActive" lowercaseString]] boolValue];
if (keepAvAudioSessionAlwaysActive) {
if ([self hasAudioSession]) {
NSError* error = nil;
if(![self.avSession setActive:YES error:&error]) {
NSLog(@"Unable to activate session: %@", [error localizedFailureReason]);
}
}
}
}
// Maps a url for a resource path for recording
- (NSURL*)urlForRecording:(NSString*)resourcePath
{
NSURL* resourceURL = nil;
NSString* filePath = nil;
NSString* docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// first check for correct extension
NSString* ext=[resourcePath pathExtension];
if ([ext caseInsensitiveCompare:@"wav"] != NSOrderedSame &&
[ext caseInsensitiveCompare:@"m4a"] != NSOrderedSame) {
resourceURL = nil;
NSLog(@"Resource for recording must have wav or m4a extension");
} else if ([resourcePath hasPrefix:DOCUMENTS_SCHEME_PREFIX]) {
// try to find Documents:// resources
filePath = [resourcePath stringByReplacingOccurrencesOfString:DOCUMENTS_SCHEME_PREFIX withString:[NSString stringWithFormat:@"%@/", docsPath]];
NSLog(@"Will use resource '%@' from the documents folder with path = %@", resourcePath, filePath);
} else if ([resourcePath hasPrefix:CDVFILE_PREFIX]) {
CDVFile *filePlugin = [self.commandDelegate getCommandInstance:@"File"];
CDVFilesystemURL *url = [CDVFilesystemURL fileSystemURLWithString:resourcePath];
filePath = [filePlugin filesystemPathForURL:url];
if (filePath == nil) {
resourceURL = [NSURL URLWithString:resourcePath];
}
} else {
if ([resourcePath hasPrefix:FILE_PREFIX]) { // Support file scheme
resourcePath = [resourcePath substringFromIndex:[FILE_PREFIX length]];
}
// if resourcePath is not from FileSystem put in tmp dir, else attempt to use provided resource path
NSString* tmpPath = [NSTemporaryDirectory()stringByStandardizingPath];
BOOL isTmp = [resourcePath rangeOfString:tmpPath].location != NSNotFound;
BOOL isDoc = [resourcePath rangeOfString:docsPath].location != NSNotFound;
if (!isTmp && !isDoc) {
// put in temp dir
filePath = [NSString stringWithFormat:@"%@/%@", tmpPath, resourcePath];
} else {
filePath = resourcePath;
}
}
if (filePath != nil) {
// create resourceURL
resourceURL = [NSURL fileURLWithPath:filePath];
}
return resourceURL;
}
- (NSString*)getCdvCustomSchemeAssetPrefix {
NSDictionary* settings = self.commandDelegate.settings;
NSString *scheme = [settings objectForKey:[@"scheme" lowercaseString]];
// If scheme is file or nil, then default to file scheme
Boolean isCdvFileScheme = [scheme isEqualToString: @"file"] || scheme == nil;
NSString *hostname = @"";
if (!isCdvFileScheme) {
if (scheme == nil || [WKWebView handlesURLScheme:scheme]) {
scheme = @"app";
}
hostname = [settings objectForKey:[@"hostname" lowercaseString]];
if(hostname == nil){
hostname = @"localhost";
}
return [NSString stringWithFormat:@"%@://%@/", scheme, hostname];
}
return nil;
}
- (Boolean)isCdvCustomSchemeUrl:(NSString*)resourcePath
{
NSString *assetUrl = [self getCdvCustomSchemeAssetPrefix];
if (assetUrl != nil) {
return [resourcePath hasPrefix:assetUrl];
}
return false;
}
// Attempts to find the file path in the "www" or "LocalFileSystem.TEMPORARY" directory.
// Used for "file://" & "custom-scheme://hostname/" schemes and leading "/" directory paths.
- (NSString*)attemptFindFilePath:(NSString*)resourcePath prefix:(NSString*)prefix
{
resourcePath = [resourcePath substringFromIndex:[prefix length]];
NSString *filePath = [self.commandDelegate pathForResource:resourcePath];
if (filePath == nil) {
// see if this exists in the documents/temp directory from a previous recording
NSString* testPath = [NSString stringWithFormat:@"%@/%@", [NSTemporaryDirectory()stringByStandardizingPath], resourcePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:testPath]) {
// inefficient as existence will be checked again below but only way to determine if file exists from previous recording
filePath = testPath;
NSLog(@"Will attempt to use file resource from LocalFileSystem.TEMPORARY directory");
} else {
// attempt to use path provided
filePath = resourcePath;
NSLog(@"Will attempt to use file resource '%@'", filePath);
}
} else {
NSLog(@"Found resource '%@' in the web folder.", filePath);
}
return filePath;
}
// Maps a url for a resource path for playing
// "Naked" resource paths are assumed to be from the www folder as its base
- (NSURL*)urlForPlaying:(NSString*)resourcePath
{
NSURL* resourceURL = nil;
NSString* filePath = nil;
// The order of checking if url path:
// 1. http:// or https://
// 2. documents://
// 3. cdvfile://
// 4. file://
// 5. custom app scheme+hostname (e.g. app://localhost/)
// 6. starts with a "/"
//
// For use case 4-6, it will attempt to find file path in "www" or "LocalFileSystem.TEMPORARY" directory
if ([resourcePath hasPrefix:HTTP_SCHEME_PREFIX] || [resourcePath hasPrefix:HTTPS_SCHEME_PREFIX]) {
// if it is a http url, use it
NSLog(@"Will use resource '%@' from the Internet.", resourcePath);
resourceURL = [NSURL URLWithString:resourcePath];
} else if ([resourcePath hasPrefix:DOCUMENTS_SCHEME_PREFIX]) {
NSString* docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
filePath = [resourcePath stringByReplacingOccurrencesOfString:DOCUMENTS_SCHEME_PREFIX withString:[NSString stringWithFormat:@"%@/", docsPath]];
NSLog(@"Will use resource '%@' from the documents folder with path = %@", resourcePath, filePath);
} else if ([resourcePath hasPrefix:CDVFILE_PREFIX]) {
CDVFile *filePlugin = [self.commandDelegate getCommandInstance:@"File"];
CDVFilesystemURL *url = [CDVFilesystemURL fileSystemURLWithString:resourcePath];
filePath = [filePlugin filesystemPathForURL:url];
if (filePath == nil) {
resourceURL = [NSURL URLWithString:resourcePath];
}
} else if ([resourcePath hasPrefix:FILE_PREFIX]) {
filePath = [self attemptFindFilePath:resourcePath prefix:FILE_PREFIX];
} else if ([self isCdvCustomSchemeUrl:resourcePath]) {
NSString *assetUrl = [self getCdvCustomSchemeAssetPrefix];
filePath = [self attemptFindFilePath:resourcePath prefix: assetUrl];
} else if ([resourcePath hasPrefix:@"/"]) {
filePath = [self attemptFindFilePath:resourcePath prefix:@"/"];
}
// if the resourcePath resolved to a file path, check that file exists
if (filePath != nil) {
// create resourceURL
resourceURL = [NSURL fileURLWithPath:filePath];
// try to access file
NSFileManager* fMgr = [NSFileManager defaultManager];
if (![fMgr fileExistsAtPath:filePath]) {
resourceURL = nil;
NSLog(@"Unknown resource '%@'", resourcePath);
}
}
return resourceURL;
}
// Creates or gets the cached audio file resource object
- (CDVAudioFile*)audioFileForResource:(NSString*)resourcePath withId:(NSString*)mediaId doValidation:(BOOL)bValidate forRecording:(BOOL)bRecord suppressValidationErrors:(BOOL)bSuppress
{
BOOL bError = NO;
CDVMediaError errcode = MEDIA_ERR_NONE_SUPPORTED;
NSString* errMsg = @"";
CDVAudioFile* audioFile = nil;
NSURL* resourceURL = nil;
if ([self soundCache] == nil) {
[self setSoundCache:[NSMutableDictionary dictionaryWithCapacity:1]];
} else {
audioFile = [[self soundCache] objectForKey:mediaId];
}
if (audioFile == nil) {
// validate resourcePath and create
if ((resourcePath == nil) || ![resourcePath isKindOfClass:[NSString class]] || [resourcePath isEqualToString:@""]) {
bError = YES;
errcode = MEDIA_ERR_ABORTED;
errMsg = @"invalid media src argument";
} else {
audioFile = [[CDVAudioFile alloc] init];
audioFile.resourcePath = resourcePath;
audioFile.resourceURL = nil; // validate resourceURL when actually play or record
[[self soundCache] setObject:audioFile forKey:mediaId];
}
}
if (bValidate && (audioFile.resourceURL == nil)) {
if (bRecord) {
resourceURL = [self urlForRecording:resourcePath];
} else {
resourceURL = [self urlForPlaying:resourcePath];
}
if ((resourceURL == nil) && !bSuppress) {
bError = YES;
errcode = MEDIA_ERR_ABORTED;
errMsg = [NSString stringWithFormat:@"Cannot use audio file from resource '%@'", resourcePath];
} else {
audioFile.resourceURL = resourceURL;
}
}
if (bError) {
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createMediaErrorWithCode:errcode message:errMsg]];
}
return audioFile;
}
// Creates or gets the cached audio file resource object
- (CDVAudioFile*)audioFileForResource:(NSString*)resourcePath withId:(NSString*)mediaId doValidation:(BOOL)bValidate forRecording:(BOOL)bRecord
{
return [self audioFileForResource:resourcePath withId:mediaId doValidation:bValidate forRecording:bRecord suppressValidationErrors:NO];
}
// returns whether or not audioSession is available - creates it if necessary
- (BOOL)hasAudioSession
{
BOOL bSession = YES;
if (!self.avSession) {
NSError* error = nil;
self.avSession = [AVAudioSession sharedInstance];
if (error) {
// is not fatal if can't get AVAudioSession , just log the error
NSLog(@"error creating audio session: %@", [[error userInfo] description]);
self.avSession = nil;
bSession = NO;
}
}
return bSession;
}
// helper function to create a error object string
- (NSDictionary*)createMediaErrorWithCode:(CDVMediaError)code message:(NSString*)message
{
NSMutableDictionary* errorDict = [NSMutableDictionary dictionaryWithCapacity:2];
[errorDict setObject:[NSNumber numberWithUnsignedInteger:code] forKey:@"code"];
[errorDict setObject:message ? message:@"" forKey:@"message"];
return errorDict;
}
//helper function to create specifically an abort error
-(NSDictionary*)createAbortError:(NSString*)message
{
return [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:message];
}
- (void)create:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command argumentAtIndex:0];
NSString* resourcePath = [command argumentAtIndex:1];
CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:YES forRecording:NO suppressValidationErrors:YES];
if (audioFile == nil) {
NSString* errorMessage = [NSString stringWithFormat:@"Failed to initialize Media file with path %@", resourcePath];
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:errorMessage]];
} else {
NSURL* resourceUrl = audioFile.resourceURL;
if (![resourceUrl isFileURL] && ![resourcePath hasPrefix:CDVFILE_PREFIX]) {
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:resourceUrl];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Subscribe to the AVPlayerItem's PlaybackStalledNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemStalledPlaying:) name:AVPlayerItemPlaybackStalledNotification object:playerItem];
// Pass the AVPlayerItem to a new player
avPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
// Avoid excessive buffering so streaming media can play instantly on iOS
// Removes preplay delay on ios 10+, makes consistent with ios9 behaviour
if ([NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10,0,0}]) {
avPlayer.automaticallyWaitsToMinimizeStalling = NO;
}
}
self.currMediaId = mediaId;
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
}
- (void)setVolume:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
#pragma unused(callbackId)
NSString* mediaId = [command argumentAtIndex:0];
NSNumber* volume = [command argumentAtIndex:1 withDefault:[NSNumber numberWithFloat:1.0]];
if ([self soundCache] != nil) {
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if (audioFile != nil) {
audioFile.volume = volume;
if (audioFile.player) {
audioFile.player.volume = [volume floatValue];
}
else {
float customVolume = [volume floatValue];
if (customVolume >= 0.0 && customVolume <= 1.0) {
[avPlayer setVolume: customVolume];
}
else {
NSLog(@"The value must be within the range of 0.0 to 1.0");
}
}
[[self soundCache] setObject:audioFile forKey:mediaId];
}
}
// don't care for any callbacks
}
- (void)setRate:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
#pragma unused(callbackId)
NSString* mediaId = [command argumentAtIndex:0];
NSNumber* rate = [command argumentAtIndex:1 withDefault:[NSNumber numberWithFloat:1.0]];
if ([self soundCache] != nil) {
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if (audioFile != nil) {
audioFile.rate = rate;
if (audioFile.player) {
audioFile.player.enableRate = YES;
audioFile.player.rate = [rate floatValue];
}
if (avPlayer.currentItem && avPlayer.currentItem.asset){
float customRate = [rate floatValue];
[avPlayer setRate:customRate];
}
[[self soundCache] setObject:audioFile forKey:mediaId];
}
}
// don't care for any callbacks
}
- (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
NSString* callbackId = command.callbackId;
#pragma unused(callbackId)
NSString* mediaId = [command argumentAtIndex:0];
NSString* resourcePath = [command argumentAtIndex:1];
NSDictionary* options = [command argumentAtIndex:2 withDefault:nil];
BOOL bError = NO;
CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:YES forRecording:NO];
if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
if (audioFile.player == nil) {
bError = [self prepareToPlay:audioFile withId:mediaId];
}
if (!bError) {
//self.currMediaId = audioFile.player.mediaId;
self.currMediaId = mediaId;
// audioFile.player != nil or player was successfully created
// get the audioSession and set the category to allow Playing when device is locked or ring/silent switch engaged
if ([self hasAudioSession]) {
NSError* __autoreleasing err = nil;
NSNumber* playAudioWhenScreenIsLocked = [options objectForKey:@"playAudioWhenScreenIsLocked"];
BOOL bPlayAudioWhenScreenIsLocked = YES;
if (playAudioWhenScreenIsLocked != nil) {
bPlayAudioWhenScreenIsLocked = [playAudioWhenScreenIsLocked boolValue];
}
NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryPlayback : AVAudioSessionCategorySoloAmbient;
[self.avSession setCategory:sessionCategory error:&err];
if (![self.avSession setActive:YES error:&err]) {
// other audio with higher priority that does not allow mixing could cause this to fail
NSLog(@"Unable to play audio: %@", [err localizedFailureReason]);
bError = YES;
}
}
if (!bError) {
NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
double duration = 0;
if (avPlayer.currentItem && avPlayer.currentItem.asset) {
CMTime time = avPlayer.currentItem.asset.duration;
duration = CMTimeGetSeconds(time);
if (isnan(duration)) {
NSLog(@"Duration is infinite, setting it to -1");
duration = -1;
}
if (audioFile.rate != nil){
float customRate = [audioFile.rate floatValue];
NSLog(@"Playing stream with AVPlayer & custom rate");
[avPlayer setRate:customRate];
} else {
NSLog(@"Playing stream with AVPlayer & default rate");
[avPlayer play];
}
} else {
NSNumber* loopOption = [options objectForKey:@"numberOfLoops"];
NSInteger numberOfLoops = 0;
if (loopOption != nil) {
numberOfLoops = [loopOption intValue] - 1;
}
audioFile.player.numberOfLoops = numberOfLoops;
if (audioFile.player.isPlaying) {
[audioFile.player stop];
audioFile.player.currentTime = 0;
}
if (audioFile.volume != nil) {
audioFile.player.volume = [audioFile.volume floatValue];
}
audioFile.player.enableRate = YES;
if (audioFile.rate != nil) {
audioFile.player.rate = [audioFile.rate floatValue];
}
[audioFile.player play];
duration = round(audioFile.player.duration * 1000) / 1000;
}
[self onStatus:MEDIA_DURATION mediaId:mediaId param:@(duration)];
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_RUNNING)];
}
}
if (bError) {
/* I don't see a problem playing previously recorded audio so removing this section - BG
NSError* error;
// try loading it one more time, in case the file was recorded previously
audioFile.player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:audioFile.resourceURL error:&error];
if (error != nil) {
NSLog(@"Failed to initialize AVAudioPlayer: %@\n", error);
audioFile.player = nil;
} else {
NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
audioFile.player.numberOfLoops = numberOfLoops;
[audioFile.player play];
} */
// error creating the session or player
[self onStatus:MEDIA_ERROR mediaId:mediaId
param:[self createMediaErrorWithCode:MEDIA_ERR_NONE_SUPPORTED message:nil]];
}
}
// else audioFile was nil - error already returned from audioFile for resource
return;
}];
}
- (BOOL)prepareToPlay:(CDVAudioFile*)audioFile withId:(NSString*)mediaId
{
BOOL bError = NO;
NSError* __autoreleasing playerError = nil;
// create the player
NSURL* resourceURL = audioFile.resourceURL;
if ([resourceURL isFileURL]) {
audioFile.player = [[CDVAudioPlayer alloc] initWithContentsOfURL:resourceURL error:&playerError];
} else {
/*
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:resourceURL];
NSString* userAgent = [self.commandDelegate userAgent];
if (userAgent) {
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
}
NSURLResponse* __autoreleasing response = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&playerError];
if (playerError) {
NSLog(@"Unable to download audio from: %@", [resourceURL absoluteString]);
} else {
// bug in AVAudioPlayer when playing downloaded data in NSData - we have to download the file and play from disk
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
NSString* filePath = [NSString stringWithFormat:@"%@/%@", [NSTemporaryDirectory()stringByStandardizingPath], uuidString];
CFRelease(uuidString);
CFRelease(uuidRef);
[data writeToFile:filePath atomically:YES];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
audioFile.player = [[CDVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&playerError];
}
*/
}
if (playerError != nil) {
NSLog(@"Failed to initialize AVAudioPlayer: %@\n", [playerError localizedDescription]);
audioFile.player = nil;
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
[self.avSession setActive:NO error:nil];
}
bError = YES;
} else {
audioFile.player.mediaId = mediaId;
audioFile.player.delegate = self;
if (avPlayer == nil)
bError = ![audioFile.player prepareToPlay];
}
return bError;
}
- (void)stopPlayingAudio:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command argumentAtIndex:0];
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if ((audioFile != nil) && (audioFile.player != nil)) {
NSLog(@"Stopped playing audio sample '%@'", audioFile.resourcePath);
[audioFile.player stop];
audioFile.player.currentTime = 0;
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
}
// seek to start and pause
if (avPlayer.currentItem && avPlayer.currentItem.asset) {
BOOL isReadyToSeek = (avPlayer.status == AVPlayerStatusReadyToPlay) && (avPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay);
if (isReadyToSeek) {
[avPlayer seekToTime: kCMTimeZero
toleranceBefore: kCMTimeZero
toleranceAfter: kCMTimeZero
completionHandler: ^(BOOL finished){
if (finished) [avPlayer pause];
}];
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
} else {
// cannot seek, wrong state
CDVMediaError errcode = MEDIA_ERR_NONE_ACTIVE;
NSString* errMsg = @"Cannot service stop request until the avPlayer is in 'AVPlayerStatusReadyToPlay' state.";
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createMediaErrorWithCode:errcode message:errMsg]];
}
}
}
- (void)pausePlayingAudio:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command argumentAtIndex:0];
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if ((audioFile != nil) && ((audioFile.player != nil) || (avPlayer != nil))) {
NSLog(@"Paused playing audio sample '%@'", audioFile.resourcePath);
if (audioFile.player != nil) {
[audioFile.player pause];
} else if (avPlayer != nil) {
[avPlayer pause];
}
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_PAUSED)];
}
}
- (void)seekToAudio:(CDVInvokedUrlCommand*)command
{
// args:
// 0 = Media id
// 1 = seek to location in milliseconds
NSString* mediaId = [command argumentAtIndex:0];
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
double position = [[command argumentAtIndex:1] doubleValue];
double posInSeconds = position / 1000;
if ((audioFile != nil) && (audioFile.player != nil)) {
if (posInSeconds >= audioFile.player.duration) {
// The seek is past the end of file. Stop media and reset to beginning instead of seeking past the end.
[audioFile.player stop];
audioFile.player.currentTime = 0;
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
} else {
audioFile.player.currentTime = posInSeconds;
[self onStatus:MEDIA_POSITION mediaId:mediaId param:@(posInSeconds)];
}
} else if (avPlayer != nil) {
int32_t timeScale = avPlayer.currentItem.asset.duration.timescale;
CMTime timeToSeek = CMTimeMakeWithSeconds(posInSeconds, timeScale);
BOOL isPlaying = (avPlayer.rate > 0 && !avPlayer.error);
BOOL isReadyToSeek = (avPlayer.status == AVPlayerStatusReadyToPlay) && (avPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay);
float currentPlaybackRate = avPlayer.rate;
// CB-10535:
// When dealing with remote files, we can get into a situation where we start playing before AVPlayer has had the time to buffer the file to be played.
// To avoid the app crashing in such a situation, we only seek if both the player and the player item are ready to play. If not ready, we send an error back to JS land.
if(isReadyToSeek) {
[avPlayer seekToTime: timeToSeek
toleranceBefore: kCMTimeZero
toleranceAfter: kCMTimeZero
completionHandler: ^(BOOL finished) {
if (isPlaying) {
[avPlayer play];
// [avPlayer play] sets the rate to 1, so we need to set it again after seeking
[avPlayer setRate:currentPlaybackRate];
};
}];
} else {
NSString* errMsg = @"AVPlayerItem cannot service a seek request with a completion handler until its status is AVPlayerItemStatusReadyToPlay.";
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:errMsg]];
}
}
}
- (void)release:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command argumentAtIndex:0];
//NSString* mediaId = self.currMediaId;
if (mediaId != nil) {
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if (audioFile != nil) {
if (audioFile.player && [audioFile.player isPlaying]) {
[audioFile.player stop];
}
if (audioFile.recorder && [audioFile.recorder isRecording]) {
[audioFile.recorder stop];
}
if (avPlayer != nil) {
[avPlayer pause];
avPlayer = nil;
}
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
[self.avSession setCategory:AVAudioSessionCategorySoloAmbient error:nil];
[self.avSession setActive:NO error:nil];
self.avSession = nil;
}
[[self soundCache] removeObjectForKey:mediaId];
NSLog(@"Media with id %@ released", mediaId);
}
}
}
- (void)getCurrentPositionAudio:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
NSString* mediaId = [command argumentAtIndex:0];
#pragma unused(mediaId)
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
double position = -1;
if ((audioFile != nil) && (audioFile.player != nil) && [audioFile.player isPlaying]) {
position = round(audioFile.player.currentTime * 1000) / 1000;
}
if (avPlayer) {
CMTime time = [avPlayer currentTime];
position = CMTimeGetSeconds(time);
}
if (isnan(position)){
position = -1;
}
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:position];
[self onStatus:MEDIA_POSITION mediaId:mediaId param:@(position)];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
- (void)startRecordingAudio:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
#pragma unused(callbackId)
NSString* mediaId = [command argumentAtIndex:0];
CDVAudioFile* audioFile = [self audioFileForResource:[command argumentAtIndex:1] withId:mediaId doValidation:YES forRecording:YES];
__block NSString* errorMsg = @"";
if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
__weak CDVSound* weakSelf = self;
void (^startRecording)(void) = ^{
NSError* __autoreleasing error = nil;
if (audioFile.recorder != nil) {
[audioFile.recorder stop];
audioFile.recorder = nil;
}
// get the audioSession and set the category to allow recording when device is locked or ring/silent switch engaged
if ([weakSelf hasAudioSession]) {
if (![weakSelf.avSession.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) {
[weakSelf.avSession setCategory:AVAudioSessionCategoryRecord error:nil];
}
if (![weakSelf.avSession setActive:YES error:&error]) {
// other audio with higher priority that does not allow mixing could cause this to fail
errorMsg = [NSString stringWithFormat:@"Unable to record audio: %@", [error localizedFailureReason]];
[weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:errorMsg]];
return;
}
}
// create a new recorder for each start record
bool isWav=[[audioFile.resourcePath pathExtension] isEqualToString:@"wav"];
NSMutableDictionary *audioSettings = [NSMutableDictionary dictionaryWithDictionary:
@{AVSampleRateKey: @(44100),
AVNumberOfChannelsKey: @(1),
}];
if (isWav) {
audioSettings[AVFormatIDKey]=@(kAudioFormatLinearPCM);
audioSettings[AVLinearPCMBitDepthKey]=@(16);
audioSettings[AVLinearPCMIsBigEndianKey]=@(false);
audioSettings[AVLinearPCMIsFloatKey]=@(false);
} else {
audioSettings[AVFormatIDKey]=@(kAudioFormatMPEG4AAC);
audioSettings[AVEncoderAudioQualityKey]=@(AVAudioQualityMedium);
}
audioFile.recorder = [[CDVAudioRecorder alloc] initWithURL:audioFile.resourceURL settings:audioSettings error:&error];
bool recordingSuccess = NO;
if (error == nil) {
audioFile.recorder.delegate = weakSelf;
audioFile.recorder.mediaId = mediaId;
audioFile.recorder.meteringEnabled = YES;
recordingSuccess = [audioFile.recorder record];
if (recordingSuccess) {
NSLog(@"Started recording audio sample '%@'", audioFile.resourcePath);
[weakSelf onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_RUNNING)];
}
}
if ((error != nil) || (recordingSuccess == NO)) {
if (error != nil) {
errorMsg = [NSString stringWithFormat:@"Failed to initialize AVAudioRecorder: %@\n", [error localizedFailureReason]];
} else {
errorMsg = @"Failed to start recording using AVAudioRecorder";
}
audioFile.recorder = nil;
if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession && ! [self isPlayingOrRecording]) {
[weakSelf.avSession setActive:NO error:nil];
}
[weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:errorMsg]];
}
};
SEL rrpSel = NSSelectorFromString(@"requestRecordPermission:");
if ([self hasAudioSession] && [self.avSession respondsToSelector:rrpSel])
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.avSession performSelector:rrpSel withObject:^(BOOL granted){
if (granted) {
startRecording();
} else {
NSString* msg = @"Error creating audio session, microphone permission denied.";
NSLog(@"%@", msg);
audioFile.recorder = nil;
if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession && ! [self isPlayingOrRecording]) {
[weakSelf.avSession setActive:NO error:nil];
}
[weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:msg]];
}
}];
#pragma clang diagnostic pop
} else {
startRecording();
}
} else {
// file did not validate
NSString* errorMsg = [NSString stringWithFormat:@"Could not record audio at '%@'", audioFile.resourcePath];
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:errorMsg]];
}
}
- (void)stopRecordingAudio:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command argumentAtIndex:0];
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if ((audioFile != nil) && (audioFile.recorder != nil)) {
NSLog(@"Stopped recording audio sample '%@'", audioFile.resourcePath);
[audioFile.recorder stop];
// no callback - that will happen in audioRecorderDidFinishRecording
}
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder*)recorder successfully:(BOOL)flag
{
CDVAudioRecorder* aRecorder = (CDVAudioRecorder*)recorder;
NSString* mediaId = aRecorder.mediaId;
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if (audioFile != nil) {
NSLog(@"Finished recording audio sample '%@'", audioFile.resourcePath);
}
if (flag) {
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
} else {
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
}
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
[self.avSession setActive:NO error:nil];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag
{
//commented as unused
CDVAudioPlayer* aPlayer = (CDVAudioPlayer*)player;
NSString* mediaId = aPlayer.mediaId;
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if (audioFile != nil) {
NSLog(@"Finished playing audio sample '%@'", audioFile.resourcePath);
}
if (flag) {
audioFile.player.currentTime = 0;
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
} else {
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
}
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
[self.avSession setActive:NO error:nil];
}
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
NSString* mediaId = self.currMediaId;
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
[self.avSession setActive:NO error:nil];
}
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
}
-(void)itemStalledPlaying:(NSNotification *) notification {
// Will be called when playback stalls due to buffer empty
NSLog(@"Stalled playback");
NSString* errMsg = @"stalled_playback";
NSString* mediaId = self.currMediaId;
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
[self createAbortError:errMsg]];
}
- (void)onMemoryWarning
{
/* https://issues.apache.org/jira/browse/CB-11513 */
NSMutableArray* keysToRemove = [[NSMutableArray alloc] init];
for(id key in [self soundCache]) {
CDVAudioFile* audioFile = [[self soundCache] objectForKey:key];
if (audioFile != nil) {
if (audioFile.player != nil && ![audioFile.player isPlaying]) {
[keysToRemove addObject:key];
}
if (audioFile.recorder != nil && ![audioFile.recorder isRecording]) {
[keysToRemove addObject:key];
}
}
}
[[self soundCache] removeObjectsForKeys:keysToRemove];
// [[self soundCache] removeAllObjects];
// [self setSoundCache:nil];
[self setAvSession:nil];
[super onMemoryWarning];
}
- (void)dealloc
{
[[self soundCache] removeAllObjects];
}
- (void)onReset
{
for (CDVAudioFile* audioFile in [[self soundCache] allValues]) {
if (audioFile != nil) {
if (audioFile.player != nil) {
[audioFile.player stop];
audioFile.player.currentTime = 0;
}
if (audioFile.recorder != nil) {
[audioFile.recorder stop];
}
}
}
[[self soundCache] removeAllObjects];
}
- (void)getCurrentAmplitudeAudio:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
NSString* mediaId = [command argumentAtIndex:0];
#pragma unused(mediaId)
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
float amplitude = 0; // The linear 0.0 .. 1.0 value
if ((audioFile != nil) && (audioFile.recorder != nil) && [audioFile.recorder isRecording]) {
[audioFile.recorder updateMeters];
float minDecibels = -60.0f; // Or use -60dB, which I measured in a silent room.
float decibels = [audioFile.recorder averagePowerForChannel:0];
if (decibels < minDecibels) {
amplitude = 0.0f;
} else if (decibels >= 0.0f) {
amplitude = 1.0f;
} else {
float root = 2.0f;
float minAmp = powf(10.0f, 0.05f * minDecibels);
float inverseAmpRange = 1.0f / (1.0f - minAmp);
float amp = powf(10.0f, 0.05f * decibels);
float adjAmp = (amp - minAmp) * inverseAmpRange;
amplitude = powf(adjAmp, 1.0f / root);
}
}
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:amplitude];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
- (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command
{
NSString* mediaId = [command argumentAtIndex:0];
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
if ((audioFile != nil) && (audioFile.recorder != nil)) {
NSLog(@"Resumed recording audio sample '%@'", audioFile.resourcePath);