cordova-plugin-nativeaudio icon indicating copy to clipboard operation
cordova-plugin-nativeaudio copied to clipboard

Ambient audio (AVAudioSessionCategoryAmbient)

Open mrnebbi opened this issue 8 years ago • 3 comments

I'm trying to get my App to play short sounds without interrupting the native audio player (if the user if playing music). The only way I can get this to work is to swap NativeAudio.m line 47 [session setCategory:AVAudioSessionCategoryPlayback error:nil]; with [session setCategory:AVAudioSessionCategoryAmbient error:nil];

Is there a way to set this from the JS? If not is this something that can be implemented easily? Failing that, is there any reason I shouldn't change that line number?

mrnebbi avatar Mar 07 '16 10:03 mrnebbi

P.s. Thanks for the Plugin!

mrnebbi avatar Mar 07 '16 10:03 mrnebbi

This is what you want :

    [session setCategory:AVAudioSessionCategoryPlayback
             withOptions:AVAudioSessionCategoryOptionMixWithOthers
                   error:nil];

pmwisdom avatar Apr 18 '16 02:04 pmwisdom

Per @pmwisdom, if you turn pluginInitialize into this you can play music and still emit sounds from your app at the same time:

- (void)pluginInitialize
{
    self.fadeMusic = NO;

    AudioSessionInitialize(NULL, NULL, nil , nil);
    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSError *setCategoryError = nil;

    // Allows the application to mix its audio with audio from other apps.
    if (![session setCategory:AVAudioSessionCategoryPlayback
                  withOptions:AVAudioSessionCategoryOptionMixWithOthers
                        error:&setCategoryError]) {

        NSLog (@"Error setting audio session category.");
        return;
    }
}

jimmy-tfp avatar Sep 12 '16 17:09 jimmy-tfp