@@ -27,8 +27,24 @@ Licensed to the Apache Software Foundation (ASF) under one
27
27
28
28
@implementation CDVSound
29
29
30
+ BOOL keepAvAudioSessionAlwaysActive = NO ;
31
+
30
32
@synthesize soundCache, avSession, currMediaId, statusCallbackId;
31
33
34
+ -(void ) pluginInitialize
35
+ {
36
+ NSDictionary * settings = self.commandDelegate .settings ;
37
+ keepAvAudioSessionAlwaysActive = [[settings objectForKey: [@" KeepAVAudioSessionAlwaysActive" lowercaseString ]] boolValue ];
38
+ if (keepAvAudioSessionAlwaysActive) {
39
+ if ([self hasAudioSession ]) {
40
+ NSError * error = nil ;
41
+ if (![self .avSession setActive: YES error: &error]) {
42
+ NSLog (@" Unable to activate session: %@ " , [error localizedFailureReason ]);
43
+ }
44
+ }
45
+ }
46
+ }
47
+
32
48
// Maps a url for a resource path for recording
33
49
- (NSURL *)urlForRecording : (NSString *)resourcePath
34
50
{
@@ -421,8 +437,8 @@ - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
421
437
[audioFile.player play];
422
438
} */
423
439
// error creating the session or player
424
- [self onStatus: MEDIA_ERROR mediaId: mediaId
425
- param: [self createMediaErrorWithCode: MEDIA_ERR_NONE_SUPPORTED message: nil ]];
440
+ [self onStatus: MEDIA_ERROR mediaId: mediaId
441
+ param: [self createMediaErrorWithCode: MEDIA_ERR_NONE_SUPPORTED message: nil ]];
426
442
}
427
443
}
428
444
// else audioFile was nil - error already returned from audioFile for resource
@@ -469,7 +485,7 @@ - (BOOL)prepareToPlay:(CDVAudioFile*)audioFile withId:(NSString*)mediaId
469
485
if (playerError != nil ) {
470
486
NSLog (@" Failed to initialize AVAudioPlayer: %@ \n " , [playerError localizedDescription ]);
471
487
audioFile.player = nil ;
472
- if (self.avSession ) {
488
+ if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [ self isPlayingOrRecording ] ) {
473
489
[self .avSession setActive: NO error: nil ];
474
490
}
475
491
bError = YES ;
@@ -600,7 +616,7 @@ - (void)release:(CDVInvokedUrlCommand*)command
600
616
[avPlayer pause ];
601
617
avPlayer = nil ;
602
618
}
603
- if (self.avSession ) {
619
+ if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [ self isPlayingOrRecording ] ) {
604
620
[self .avSession setActive: NO error: nil ];
605
621
self.avSession = nil ;
606
622
}
@@ -705,7 +721,7 @@ - (void)startRecordingAudio:(CDVInvokedUrlCommand*)command
705
721
errorMsg = @" Failed to start recording using AVAudioRecorder" ;
706
722
}
707
723
audioFile.recorder = nil ;
708
- if (weakSelf.avSession ) {
724
+ if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession && ! [ self isPlayingOrRecording ] ) {
709
725
[weakSelf.avSession setActive: NO error: nil ];
710
726
}
711
727
[weakSelf onStatus: MEDIA_ERROR mediaId: mediaId param:
@@ -725,7 +741,7 @@ - (void)startRecordingAudio:(CDVInvokedUrlCommand*)command
725
741
NSString * msg = @" Error creating audio session, microphone permission denied." ;
726
742
NSLog (@" %@ " , msg);
727
743
audioFile.recorder = nil ;
728
- if (weakSelf.avSession ) {
744
+ if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession && ! [ self isPlayingOrRecording ] ) {
729
745
[weakSelf.avSession setActive: NO error: nil ];
730
746
}
731
747
[weakSelf onStatus: MEDIA_ERROR mediaId: mediaId param:
@@ -773,7 +789,7 @@ - (void)audioRecorderDidFinishRecording:(AVAudioRecorder*)recorder successfully:
773
789
[self onStatus: MEDIA_ERROR mediaId: mediaId param:
774
790
[self createMediaErrorWithCode: MEDIA_ERR_DECODE message: nil ]];
775
791
}
776
- if (self.avSession ) {
792
+ if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [ self isPlayingOrRecording ] ) {
777
793
[self .avSession setActive: NO error: nil ];
778
794
}
779
795
}
@@ -795,16 +811,16 @@ - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)fl
795
811
[self onStatus: MEDIA_ERROR mediaId: mediaId param:
796
812
[self createMediaErrorWithCode: MEDIA_ERR_DECODE message: nil ]];
797
813
}
798
- if (self.avSession ) {
799
- [self .avSession setActive: NO error: nil ];
814
+ if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [ self isPlayingOrRecording ] ) {
815
+ [self .avSession setActive: NO error: nil ];
800
816
}
801
817
}
802
818
803
819
-(void )itemDidFinishPlaying : (NSNotification *) notification {
804
820
// Will be called when AVPlayer finishes playing playerItem
805
821
NSString * mediaId = self.currMediaId ;
806
822
807
- if (self.avSession ) {
823
+ if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [ self isPlayingOrRecording ] ) {
808
824
[self .avSession setActive: NO error: nil ];
809
825
}
810
826
[self onStatus: MEDIA_STATE mediaId: mediaId param: @(MEDIA_STOPPED)];
@@ -917,7 +933,7 @@ - (void)onStatus:(CDVMediaMsg)what mediaId:(NSString*)mediaId param:(NSObject*)p
917
933
status[@" msgType" ] = @(what);
918
934
// in the error case contains a dict with "code" and "message"
919
935
// otherwise a NSNumber
920
- status[@" value" ] = param;
936
+ status[@" value" ] = param;
921
937
status[@" id" ] = mediaId;
922
938
NSMutableDictionary * dict=[NSMutableDictionary dictionary ];
923
939
dict[@" action" ] = @" status" ;
@@ -931,12 +947,26 @@ - (void)onStatus:(CDVMediaMsg)what mediaId:(NSString*)mediaId param:(NSObject*)p
931
947
param=[[NSString alloc ] initWithData: jsonData encoding: NSUTF8StringEncoding];
932
948
}
933
949
NSString * jsString = [NSString stringWithFormat: @" %@ (\" %@ \" ,%d ,%@ );" ,
934
- @" cordova.require('cordova-plugin-media.Media').onStatus" ,
950
+ @" cordova.require('cordova-plugin-media.Media').onStatus" ,
935
951
mediaId, (int )what, param];
936
952
[self .commandDelegate evalJs: jsString];
937
953
}
938
954
}
939
955
956
+ -(BOOL ) isPlayingOrRecording
957
+ {
958
+ for (NSString * mediaId in soundCache) {
959
+ CDVAudioFile* audioFile = [soundCache objectForKey: mediaId];
960
+ if (audioFile.player && [audioFile.player isPlaying ]) {
961
+ return true ;
962
+ }
963
+ if (audioFile.recorder && [audioFile.recorder isRecording ]) {
964
+ return true ;
965
+ }
966
+ }
967
+ return false ;
968
+ }
969
+
940
970
@end
941
971
942
972
@implementation CDVAudioFile
0 commit comments