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:#2110 deactivate the old session when playing again #2111

Merged
merged 1 commit into from
Sep 22, 2023
Merged

fix:#2110 deactivate the old session when playing again #2111

merged 1 commit into from
Sep 22, 2023

Conversation

mvn-haiphung-dn
Copy link
Contributor

Fix #2110

@dcvz
Copy link
Contributor

dcvz commented Sep 4, 2023

@mvn-haiphung-dn I just merged in #2091 -- would you mind seeing if this is still an issue with those fixes?

@kirillzyusko
Copy link
Contributor

@dcvz I think we've tried to use latest release (RC08) and we still have this problem. It seems like only deactivating session and activating it back resolves the problem 😔

@dcvz
Copy link
Contributor

dcvz commented Sep 14, 2023

thanks for confirming @kirillzyusko <3

@dcvz dcvz merged commit 4ba79d0 into doublesymmetry:main Sep 22, 2023
3 checks passed
@puckey
Copy link
Collaborator

puckey commented Oct 3, 2023

@kirillzyusko can you describe the problem you were seeing? What Unity game was involved?

@kirillzyusko
Copy link
Contributor

@puckey it was very hard to isolate the problem that we had, because Unity games are developed by another company and it's not open source code, so we were not able to provide a repro.

Basically steps to reproduce were described in #2110 - when you play an audio after you played a game you will not see a song in notification center.

I've tried to debug this problem a little bit - from my observation our Unity module just changes category to ambient on mount and set it to playback when game gets closed:

#if TARGET_IPHONE_SIMULATOR

#else
#import "ReactNativeUnityView.h"
#import "ReactNativeUnity.h"
#import <AVFAudio/AVAudioSession.h>

NSDictionary* appLaunchOpts;

@implementation ReactNativeUnityView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [ReactNativeUnityView changeAudioSessionCategory:AVAudioSessionCategoryAmbient]; // <- ambient on mount
        _uView = [[[ReactNativeUnity launchWithOptions:appLaunchOpts] appController] rootView];
        [FrameworkLibAPI registerAPIforNativeCalls:self];
    }
    return self;
}

+ (void)changeAudioSessionCategory:(AVAudioSessionCategory)category {
    // schedule async operation and execute it as soon as possible
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        NSError *setCategoryErr = nil;
        NSError *activationErr  = nil;
        [[AVAudioSession sharedInstance] setCategory:category error:&setCategoryErr];
        [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
        NSLog(@"AVAudioSession changed. Category error: %@. Activation error: %@", [setCategoryErr localizedDescription], [activationErr localizedDescription]);
    });
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [_uView removeFromSuperview];
    _uView.frame = self.bounds;
    [self insertSubview:_uView atIndex:0];
}

+ (void)pauseUnity:(BOOL * _Nonnull)pause
{
    [[ReactNativeUnity ufw] pause:pause];
}

+ (void)unloadUnity
{
    UIWindow * main = [[[UIApplication sharedApplication] delegate] window];
    if(main != nil) {
        [main makeKeyAndVisible];
        if ([ReactNativeUnity ufw]) {
            [self changeAudioSessionCategory:AVAudioSessionCategoryPlayback]; // <- playback on unmount
            [[ReactNativeUnity ufw] unregisterFrameworkListener:[ReactNativeUnity ufw]];
            [[ReactNativeUnity ufw] unloadApplication];
        }
    }
}

+ (void)UnityPostMessage:(NSString*)gameObject methodName:(NSString*)methodName message:(NSString*) message {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[ReactNativeUnity ufw] sendMessageToGOWithName:[gameObject UTF8String] functionName:[methodName UTF8String] message:[message UTF8String]];
    });
}

- (void)sendMessageToMobileApp:(NSString *)message {
    if (self.onUnityMessage) {
        NSDictionary* data = @{
            @"message": message
        };

        self.onUnityMessage(data);
    }
}

@end

#endif

I've tried to add setAmbient/setPlayback methods in RNTP (and call them by pressing on a button) to reproduce the problem, but it didn't work out - everything was working fine 🤷‍♂️ Maybe there are other conflicts with unity module and RNTP implementation, but I wasn't able to identify them.

Are you concerned by this fix? Or maybe want us to test any upcoming PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants