Announcement

Collapse
No announcement yet.

How can I set AVAudioSession for iOS apps to allow device/apps to continue audio playback?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • replied
    Originally posted by Platinum Angel View Post

    any chance you can tell us how you did it?

    For those needing an answer regarding the observer ios class, im pasting a template code




    Code:
    @implementation NotificationObserver
    
    static NotificationObserver* NotificationObserverInstance = nil;
    
    -(id)init
    {
        self = [super init];
        return self;
    }
    
    +(void)load
    {
        [super load];
    
        if (NotificationObserverInstance == nil)
        {
            NotificationObserverInstance = [[NotificationObserver alloc] init];
            [NotificationObserverInstance registerLifeCycleListener];
        }
    }
    
    +(NotificationObserver*)sharedInstance
    {
        if (NotificationObserverInstance == nil)
        {
            [NotificationObserver load];
        }
    
        return NotificationObserverInstance;
    }
    
    -(void)registerLifeCycleListener
    {
        [[NSNotificationCenter defaultCenter] addObserver:self    selector : @selector(didFinishLaunching : )    name:UIApplicationDidFinishLaunchingNotification object : nil];
    
    
        [[NSNotificationCenter defaultCenter] addObserver:self    selector : @selector(appWillResignActive : )    name:UIApplicationWillResignActiveNotification object : nil];
        [[NSNotificationCenter defaultCenter] addObserver:self    selector : @selector(appDidBecomeActive : )    name:UIApplicationDidBecomeActiveNotification object : nil];
    
    }
    
    
    
    +(void)unregisterObserver
    {
        if (NotificationObserverInstance != nil)
        {
            [[NSNotificationCenter defaultCenter] removeObserver:self name : UIApplicationDidFinishLaunchingNotification object : nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self name : UIApplicationWillResignActiveNotification object : nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self name : UIApplicationDidBecomeActiveNotification object : nil];
        }
    }
    
    
    -(void)appDidBecomeActive:(NSNotification*)notification
    {
    }
    
    -(void)appWillResignActive : (NSNotification*)notification
    {
    
    }
    
    
    
    -(void)didFinishLaunching:(NSNotification*)notification
    {
        [NotificationObserver requestAuthorization];
    }
    
    +(void)requestAuthorization
    {
    
    
    }
    
    @end



    Then in the plugin's startupmodule() function, paste the below :

    [NotificationObserver load];



    Calling it in the startupmodule() function ensures the observer is called right at the start of the game launch.

    Leave a comment:


  • replied
    Originally posted by codehawk64 View Post

    i just found a way to invoke that function without modifying the engine, by using an observer ios class.
    any chance you can tell us how you did it?

    Leave a comment:


  • replied
    Originally posted by TheNebelung View Post






    No Problem at all Lone. Your questions helped me figure it out.







    I am not sure. I believe this is an option in 4.24 now but I heard it is a mobile nightmare so i haven't upgraded. I don't know much about C++ coding either. Its not too hard to read but writing it is beyond me. This was the only way could figure out though that didn't break all of my sound in game.
    i just found a way to invoke that function without modifying the engine, by using an observer ios class.

    Leave a comment:


  • replied


    Originally posted by LoneScrapbot View Post

    Thanks very much for this! VERY much appreciated.
    I have been meaning to follow this up for some time and intended to raise it as a bug for those of us who are not compiling from source. I'll try and get my act together and post back here if I hear back from Epic bug reports.



    No Problem at all Lone. Your questions helped me figure it out.




    Originally posted by codehawk64 View Post

    Hi, is it not possible to create a plugin to create a new AppDelegate and override the "didFinishLaunchingWithOptions" ? I want to avoid editing and rebuilding the engine.

    I am not sure. I believe this is an option in 4.24 now but I heard it is a mobile nightmare so i haven't upgraded. I don't know much about C++ coding either. Its not too hard to read but writing it is beyond me. This was the only way could figure out though that didn't break all of my sound in game.

    Leave a comment:


  • replied
    Originally posted by TheNebelung View Post
    Hey there, So i figured out your issue. This fix only works if you are building from source though. This is with 4.23.1 in mind just incase things changed from previous engine versions but it will be the same for most versions.

    So the file you need is IOSAppDelegate.CPP

    The location of the file is (Folder Where Source Built Engine Is) -> (Engine) -> (Source) -> (Runtime) -> (ApplicationCore) -> (Private) -> (IOS) -> IOSAppDelegate.cpp

    You can use the search function as well to find it quickly. Once you open it, you can go down to line 976 where it mentions "didFinishLaunchingWithOptions". Right underneath that, copy and paste this line of code:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

    Thats it. Compile and Build your game. I tested this in a fully production ready game and my sounds, voices, and background music were all fine to play like normal but when I opened the game, it definitely didn't stop my music from playing. It also still obeys the mute button on the iPhone so the music will still play but your game music will stay quiet. You actually helped me figure out this 2 year long issue so I figured I would return the favor.

    Hopefully it helps others as well.

    Here is a picture of the code just in case.

    Click image for larger version Name:	PictureToStopStoppingMusicFromPlayingWhenOpeningIOSApp.png Views:	0 Size:	113.0 KB ID:	1686020
    Hi, is it not possible to create a plugin to create a new AppDelegate and override the "didFinishLaunchingWithOptions" ? I want to avoid editing and rebuilding the engine.

    Leave a comment:


  • replied
    Originally posted by TheNebelung View Post
    Hey there, So i figured out your issue. This fix only works if you are building from source though. This is with 4.23.1 in mind just incase things changed from previous engine versions but it will be the same for most versions.

    So the file you need is IOSAppDelegate.CPP

    The location of the file is (Folder Where Source Built Engine Is) -> (Engine) -> (Source) -> (Runtime) -> (ApplicationCore) -> (Private) -> (IOS) -> IOSAppDelegate.cpp

    You can use the search function as well to find it quickly. Once you open it, you can go down to line 976 where it mentions "didFinishLaunchingWithOptions". Right underneath that, copy and paste this line of code:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

    Thats it. Compile and Build your game. I tested this in a fully production ready game and my sounds, voices, and background music were all fine to play like normal but when I opened the game, it definitely didn't stop my music from playing. It also still obeys the mute button on the iPhone so the music will still play but your game music will stay quiet. You actually helped me figure out this 2 year long issue so I figured I would return the favor.

    Hopefully it helps others as well.

    Here is a picture of the code just in case.

    Click image for larger version Name:	PictureToStopStoppingMusicFromPlayingWhenOpeningIOSApp.png Views:	0 Size:	113.0 KB ID:	1686020
    Thanks very much for this! VERY much appreciated.
    I have been meaning to follow this up for some time and intended to raise it as a bug for those of us who are not compiling from source. I'll try and get my act together and post back here if I hear back from Epic bug reports.

    Leave a comment:


  • replied
    Hey there, So i figured out your issue. This fix only works if you are building from source though. This is with 4.23.1 in mind just incase things changed from previous engine versions but it will be the same for most versions.

    So the file you need is IOSAppDelegate.CPP

    The location of the file is (Folder Where Source Built Engine Is) -> (Engine) -> (Source) -> (Runtime) -> (ApplicationCore) -> (Private) -> (IOS) -> IOSAppDelegate.cpp

    You can use the search function as well to find it quickly. Once you open it, you can go down to line 976 where it mentions "didFinishLaunchingWithOptions". Right underneath that, copy and paste this line of code:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

    Thats it. Compile and Build your game. I tested this in a fully production ready game and my sounds, voices, and background music were all fine to play like normal but when I opened the game, it definitely didn't stop my music from playing. It also still obeys the mute button on the iPhone so the music will still play but your game music will stay quiet. You actually helped me figure out this 2 year long issue so I figured I would return the favor.

    Hopefully it helps others as well.

    Here is a picture of the code just in case.

    Click image for larger version  Name:	PictureToStopStoppingMusicFromPlayingWhenOpeningIOSApp.png Views:	0 Size:	113.0 KB ID:	1686020
    Last edited by Loreliya; 11-12-2019, 10:42 AM.

    Leave a comment:


  • How can I set AVAudioSession for iOS apps to allow device/apps to continue audio playback?

    In a UE4 project, is it possible to set the AVAudioSession category as described here ? Or perhaps something else that has a similar result? I'd like my iOS app NOT to take over the audio session when launched, but to allow background audio to continue (podcasts etc). Currently when I launch my UE4 iOS apps, that app causes all currently playing audio to stop.
Working...
X