Skip to content

Commit a7e376a

Browse files
adriano-di-giovannimacdonst
authored andcommitted
CB-7684 (#143)
* do not deactivate avsession when items finish playing * CB76-84: check if any audio file is playing or recording before deactivating the avsession * CB-7684: add ios preference to keep avsession always active * CB-7684: change variable name to uppercase * CB-7684: change variable name to uppercase in CDVSound.m, too * CB-7684: fix plugin definition * CB-7684: activate avsession during plugin initialization * update plugin definition to solve merge conflict * update plugin definition to fix merge conflict * update plugin definition to fix merge conflict
1 parent 4b89d80 commit a7e376a

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ id="cordova-plugin-media"
6666

6767
<!-- ios -->
6868
<platform name="ios">
69+
<preference name="KEEP_AVAUDIOSESSION_ALWAYS_ACTIVE" default="NO" />
6970
<config-file target="config.xml" parent="/*">
7071
<feature name="Media">
7172
<param name="ios-package" value="CDVSound" />
7273
</feature>
74+
<preference
75+
name="KeepAVAudioSessionAlwaysActive"
76+
value="$KEEP_AVAUDIOSESSION_ALWAYS_ACTIVE" />
7377
</config-file>
7478
<header-file src="src/ios/CDVSound.h" />
7579
<source-file src="src/ios/CDVSound.m" />

src/ios/CDVSound.m

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,24 @@ Licensed to the Apache Software Foundation (ASF) under one
2727

2828
@implementation CDVSound
2929

30+
BOOL keepAvAudioSessionAlwaysActive = NO;
31+
3032
@synthesize soundCache, avSession, currMediaId, statusCallbackId;
3133

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+
3248
// Maps a url for a resource path for recording
3349
- (NSURL*)urlForRecording:(NSString*)resourcePath
3450
{
@@ -421,7 +437,7 @@ - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
421437
[audioFile.player play];
422438
} */
423439
// error creating the session or player
424-
[self onStatus:MEDIA_ERROR mediaId:mediaId
440+
[self onStatus:MEDIA_ERROR mediaId:mediaId
425441
param:[self createMediaErrorWithCode:MEDIA_ERR_NONE_SUPPORTED message:nil]];
426442
}
427443
}
@@ -469,7 +485,7 @@ - (BOOL)prepareToPlay:(CDVAudioFile*)audioFile withId:(NSString*)mediaId
469485
if (playerError != nil) {
470486
NSLog(@"Failed to initialize AVAudioPlayer: %@\n", [playerError localizedDescription]);
471487
audioFile.player = nil;
472-
if (self.avSession) {
488+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
473489
[self.avSession setActive:NO error:nil];
474490
}
475491
bError = YES;
@@ -600,7 +616,7 @@ - (void)release:(CDVInvokedUrlCommand*)command
600616
[avPlayer pause];
601617
avPlayer = nil;
602618
}
603-
if (self.avSession) {
619+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
604620
[self.avSession setActive:NO error:nil];
605621
self.avSession = nil;
606622
}
@@ -705,7 +721,7 @@ - (void)startRecordingAudio:(CDVInvokedUrlCommand*)command
705721
errorMsg = @"Failed to start recording using AVAudioRecorder";
706722
}
707723
audioFile.recorder = nil;
708-
if (weakSelf.avSession) {
724+
if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession && ! [self isPlayingOrRecording]) {
709725
[weakSelf.avSession setActive:NO error:nil];
710726
}
711727
[weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
@@ -725,7 +741,7 @@ - (void)startRecordingAudio:(CDVInvokedUrlCommand*)command
725741
NSString* msg = @"Error creating audio session, microphone permission denied.";
726742
NSLog(@"%@", msg);
727743
audioFile.recorder = nil;
728-
if (weakSelf.avSession) {
744+
if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession && ! [self isPlayingOrRecording]) {
729745
[weakSelf.avSession setActive:NO error:nil];
730746
}
731747
[weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
@@ -773,7 +789,7 @@ - (void)audioRecorderDidFinishRecording:(AVAudioRecorder*)recorder successfully:
773789
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
774790
[self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
775791
}
776-
if (self.avSession) {
792+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
777793
[self.avSession setActive:NO error:nil];
778794
}
779795
}
@@ -795,18 +811,18 @@ - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)fl
795811
[self onStatus:MEDIA_ERROR mediaId:mediaId param:
796812
[self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
797813
}
798-
if (self.avSession) {
799-
[self.avSession setActive:NO error:nil];
800-
}
814+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
815+
[self.avSession setActive:NO error:nil];
816+
}
801817
}
802818

803819
-(void)itemDidFinishPlaying:(NSNotification *) notification {
804820
// Will be called when AVPlayer finishes playing playerItem
805821
NSString* mediaId = self.currMediaId;
806822

807-
if (self.avSession) {
808-
[self.avSession setActive:NO error:nil];
809-
}
823+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
824+
[self.avSession setActive:NO error:nil];
825+
}
810826
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
811827
}
812828

@@ -917,7 +933,7 @@ - (void)onStatus:(CDVMediaMsg)what mediaId:(NSString*)mediaId param:(NSObject*)p
917933
status[@"msgType"] = @(what);
918934
//in the error case contains a dict with "code" and "message"
919935
//otherwise a NSNumber
920-
status[@"value"] = param;
936+
status[@"value"] = param;
921937
status[@"id"] = mediaId;
922938
NSMutableDictionary* dict=[NSMutableDictionary dictionary];
923939
dict[@"action"] = @"status";
@@ -931,12 +947,26 @@ - (void)onStatus:(CDVMediaMsg)what mediaId:(NSString*)mediaId param:(NSObject*)p
931947
param=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
932948
}
933949
NSString* jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);",
934-
@"cordova.require('cordova-plugin-media.Media').onStatus",
950+
@"cordova.require('cordova-plugin-media.Media').onStatus",
935951
mediaId, (int)what, param];
936952
[self.commandDelegate evalJs:jsString];
937953
}
938954
}
939955

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+
940970
@end
941971

942972
@implementation CDVAudioFile

0 commit comments

Comments
 (0)