Skip to content

Commit 7693701

Browse files
author
Buddy Reno
committed
Add PR apache#143 to fix iOS I/O error
1 parent e1dc539 commit 7693701

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

plugin.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,19 @@ id="cordova-plugin-media"
102102
<config-file target="config.xml" parent="/*">
103103
<feature name="Media">
104104
<param name="ios-package" value="CDVSound" />
105+
<param name="onload" value="true"/>
105106
</feature>
106107
</config-file>
107108
<header-file src="src/ios/CDVSound.h" />
108109
<source-file src="src/ios/CDVSound.m" />
110+
<preference name="MICROPHONE_USAGE_DESCRIPTION" default=" " />
111+
<config-file target="*-Info.plist" parent="NSMicrophoneUsageDescription">
112+
<string>$MICROPHONE_USAGE_DESCRIPTION</string>
113+
</config-file>
114+
<preference name="KEEP_AVAUDIOSESSION_ALWAYS_ACTIVE" default="NO" />
115+
<config-file target="config.xml" parent="/*">
116+
<preference name="KeepAVAudioSessionAlwaysActive" value="$KEEP_AVAUDIOSESSION_ALWAYS_ACTIVE" />
117+
</config-file>
109118
</platform>
110119

111120
<!-- blackberry10 -->

src/ios/CDVSound.m

Lines changed: 42 additions & 12 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,8 +437,8 @@ - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
421437
[audioFile.player play];
422438
} */
423439
// 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]];
426442
}
427443
}
428444
// else audioFile was nil - error already returned from audioFile for resource
@@ -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,16 +811,16 @@ - (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];
814+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
815+
[self.avSession setActive:NO error:nil];
800816
}
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) {
823+
if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording]) {
808824
[self.avSession setActive:NO error:nil];
809825
}
810826
[self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
@@ -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)