Search Unity

Audio [SOLVED] How Do I Keep My Music Player Active, When iOS App goes to sleep?

Discussion in 'Audio & Video' started by DrMarvellous, Mar 14, 2019.

  1. DrMarvellous

    DrMarvellous

    Joined:
    Feb 5, 2019
    Posts:
    10
    Hi there, I've been trying for a few days now to keep a music player app running in the background on an iOS build, but each time my device goes to sleep, the music stops.

    It's an audio trail and mainly a voiceover is playing with some music for intro and outro pieces, but I cannot find anywhere how to keep the audio running.

    Several searches have recommended the following:

    public class ExampleClass : MonoBehaviour {

    void Example() {

    Application.runInBackground = true;

    }

    }


    but I added that to my AppManager script, which handles all my audio files, but still no success.

    Also, (newbie question) where would I add the code required to keep the app running in the background? Can I add it to my AppManager script, or would I need to create a new script and attach that to something else in my project?

    Many thanks for any assistance anyone can provide, I'm really stuck with this one.

    Cheers.
     
  2. DrMarvellous

    DrMarvellous

    Joined:
    Feb 5, 2019
    Posts:
    10
    **SOLVED** Unity 2018.2.7f1 Xcode 10.1
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    I'm happy to report, that after a few days of searching, I was able to my media players audio playing in the background, after the app went into sleep mode, or if the home button was pressed. Here's how I did it.

    Disclaimer - I'm a newbie, to both Unity and the forum, so I'm still not 100% sure if all this code is written as good as it could be, but it worked for me and I hope it works for someone else who is looking to solve the same difficulties.

    ~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~

    1: In Unity, PlayerSettings --> "Behaviour in Background" = "Custom".

    2: Export Unity Build to Xcode.

    3: In Xcode, find UnityAppController.mm and find this code:
    ~~~~~~~~~~~~~~~~~~

    - (void)startUnity:(UIApplication*)application

    {

    NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");

    UnityInitApplicationGraphics();

    // we make sure that first level gets correct display list and orientation

    [[DisplayManager Instance] updateDisplayListCacheInUnity];

    UnityLoadApplication();

    Profiler_InitProfiler();

    [self showGameUI];

    [self createDisplayLink];

    UnitySetPlayerFocus(1);

    ~~~~~~~~~~~~~~~~~~

    Underneath this code, add the following:

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

    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    }
    ~~~~~~~~~~~~~~~~~~

    So it should end up looking like this:

    - (void)startUnity:(UIApplication*)application
    {
    NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");

    UnityInitApplicationGraphics();

    // we make sure that first level gets correct display list and orientation

    [[DisplayManager Instance] updateDisplayListCacheInUnity];

    UnityLoadApplication();

    Profiler_InitProfiler();

    [self showGameUI];

    [self createDisplayLink];

    UnitySetPlayerFocus(1);

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

    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    }
    ~~~~~~~~~~~~~~~~~~

    To complete, the coding part, I was told to also add

    @IMpoRt AVFoundation;

    to the UnityAppController.mm declaration section, (for me, this was at the top, near all the other import calls were), but that didn’t work for me, as I think it’s now outdate in the Unity version I’m using Unity 2018.2.7f1.

    So I manually started to type in the code and Xcode suggested the following:

    #import <AVFoundation/AVFoundation.h>

    This code worked for me.

    (Side Note)
    If you’re using an older version of Unity and you’re successful with the “@import” method, you may also need to enable Modules, to avoid additional warnings:

    Enable the modules in Xcode. Go to the Target > Build Settings and set the Enable Modules (C and Objective-C modules) to YES. You may also need to do this in the target too.

    4: There was an error following a project clean and build, so this final step was necessary. I was getting a “Thread 1breakpoint 1.1” in my line of code error and the simple fix was “Deactivate Breakpoints” inside the “Debug menu” in Xcode.

    5: Did one final clean and build in Xcode, sent the build to my iOS device and the audio worked perfectly in the background after the app went to sleep, or the home button was pressed - bravo!

    Hope this helps?
     
  3. Chris_1001

    Chris_1001

    Joined:
    Jan 16, 2018
    Posts:
    13
    Tested this and it works with the latest Unity and iOs - nice work!
    It works so well, I had to put in an off button for the audio,
    as otherwise the only way to silence the app is to force close it.

    Does anyone know how to do the same for the Screen - I want to keep the app / screen awake for 10 minutes.
    Not sure if iOs allows it - but if it can be done for audio - maybe for the screen too?
     
  4. Chris_1001

    Chris_1001

    Joined:
    Jan 16, 2018
    Posts:
    13
  5. sebastian_PE

    sebastian_PE

    Joined:
    Jul 22, 2014
    Posts:
    7
    Hi Chris, I'm trying to do what you have accomplished. I'm completely new to Unity and I don't find the settings for the behaviour in the background. I'd appreciate your help!
     
    Last edited: May 4, 2020
  6. Deleted User

    Deleted User

    Guest

    Trying to do so; for me it doesn't work (i'm processing audio from network). Any ideas ?
     
  7. eduardolago

    eduardolago

    Joined:
    Oct 3, 2020
    Posts:
    1
    It doesn't work for me either. iPhone 8, Sofware 13.7