Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix background music stop when the plugin is initialized #117

Merged
merged 1 commit into from
Mar 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/ios/NativeAudio.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
//
//
// NativeAudio.m
// NativeAudio
//
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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])
{
Expand All @@ -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];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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],
Expand All @@ -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
@end