From 37bc8e6ee18cecb68cf47e368b01bc030a8215a7 Mon Sep 17 00:00:00 2001 From: Thib Date: Fri, 3 Mar 2017 18:36:54 +0100 Subject: [PATCH] Fix background music stop when the plugin is initialized --- src/ios/NativeAudio.m | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/ios/NativeAudio.m b/src/ios/NativeAudio.m index af8d5e38..e797d1c9 100644 --- a/src/ios/NativeAudio.m +++ b/src/ios/NativeAudio.m @@ -1,5 +1,5 @@ // -// +// // NativeAudio.m // NativeAudio // @@ -31,7 +31,8 @@ - (void)pluginInitialize AudioSessionInitialize(NULL, NULL, nil , nil); AVAudioSession *session = [AVAudioSession sharedInstance]; - + // we activate the audio session after the options to mix with others is set + [session setActive: NO error: nil]; NSError *setCategoryError = nil; // Allows the application to mix its audio with audio from other apps. @@ -150,7 +151,7 @@ - (void) preloadComplex:(CDVInvokedUrlCommand *)command } else { voices = [NSNumber numberWithInt:1]; } - + NSNumber *delay = nil; if ( [arguments count] > 4 && [arguments objectAtIndex:4] != [NSNull null]) { @@ -175,7 +176,7 @@ - (void) preloadComplex:(CDVInvokedUrlCommand *)command withVoices:voices withVolume:volume withFadeDelay:delay]; - + audioMapping[audioID] = asset; NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", INFO_ASSET_LOADED, audioID]; @@ -344,16 +345,16 @@ - (void) unload:(CDVInvokedUrlCommand *)command NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", ERROR_REFERENCE_MISSING, audioID]; [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: RESULT] callbackId:callbackId]; } - + [audioMapping removeObjectForKey: audioID]; - + NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", INFO_ASSET_UNLOADED, audioID]; [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: RESULT] callbackId:callbackId]; } else { NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", ERROR_REFERENCE_MISSING, audioID]; [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: RESULT] callbackId:callbackId]; } - + } - (void) setVolumeForComplexAsset:(CDVInvokedUrlCommand *)command @@ -424,7 +425,7 @@ static void (mySystemSoundCompletionProc)(SystemSoundID ssID,void* clientData) NSString *audioID = [temp lastObject]; [nativeAudio sendCompleteCallback:audioID]; - + // Cleanup, these cb are one-shots AudioServicesRemoveSystemSoundCompletion(ssID); } @@ -434,25 +435,25 @@ - (void) addCompleteListener:(CDVInvokedUrlCommand *)command NSString *callbackId = command.callbackId; NSArray* arguments = command.arguments; NSString *audioID = [arguments objectAtIndex:0]; - + [self.commandDelegate runInBackground:^{ if (audioMapping) { - + NSObject* asset = audioMapping[audioID]; - + if (asset != nil){ - + if(completeCallbacks == nil) { completeCallbacks = [NSMutableDictionary dictionary]; } completeCallbacks[audioID] = command.callbackId; - + if ([asset isKindOfClass:[NativeAudioAsset class]]) { NativeAudioAsset *_asset = (NativeAudioAsset*) asset; [_asset setCallbackAndId:^(NSString* audioID) { [self sendCompleteCallback:audioID]; } audioId:audioID]; - + } else if ( [asset isKindOfClass:[NSNumber class]] ) { NSNumber *_asset = (NSNumber*) asset; AudioServicesAddSystemSoundCompletion([_asset intValue], @@ -462,17 +463,17 @@ - (void) addCompleteListener:(CDVInvokedUrlCommand *)command (__bridge void *)(self)); } } else { - + NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", ERROR_REFERENCE_MISSING, audioID]; [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: RESULT] callbackId:callbackId]; } - + } else { - + NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", ERROR_REFERENCE_MISSING, audioID]; [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: RESULT] callbackId:callbackId]; } }]; } -@end \ No newline at end of file +@end