Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Keep audio playing with focus lost?

Discussion in 'Audio & Video' started by Egil-Sandfeld, May 24, 2015.

  1. Egil-Sandfeld

    Egil-Sandfeld

    Joined:
    Oct 8, 2012
    Posts:
    72
    Application.runInBackground = true keeps the Unity app active and run in background. What it does not is letting the audio play.

    I'm using Wwise and Unity to add a few audio effects to a racing simulator. The simulator outputs live telemetry which I use with Wwise to alter certain sounds. This all works fine when the Unity Editor runs (no matter On or Lost focus). It also works if I build the app and keep the built app window in focus.

    Audio is however lost (Wwise reports "Audio Thread Suspended") if I click outside of the app window or enter the racing simulator. I can see that the built app still runs (GUI) but no sound comes through.

    Is it possible to fix this, so the audio keeps playing despite losing focus?
     
    dan_ginovker likes this.
  2. The_Natural_Tanuki

    The_Natural_Tanuki

    Joined:
    Oct 14, 2015
    Posts:
    6
    I want to do the same thing !

    My app is for android and I don't have the answer but I will look into Android permissions.
     
  3. thisoneyouknow

    thisoneyouknow

    Joined:
    Dec 15, 2016
    Posts:
    1
    Hello, i know this is an old post, but i just found a fix for this very problem.
    So if anyone stumbles upon this issue, this is how you deal with it :
    1) In Unity, locate your WwiseGlobal game object.
    2) In the inspector, edit the Ak Initializer (Script)
    3) In your script editor, you have to comment from l 295 to 330 as follows :

    /*#if !UNITY_EDITOR && !UNITY_WIIU && !UNITY_IOS
    //Keep out of UNITY_EDITOR because the sound needs to keep playing when switching windows (remote debugging in Wwise, for example).
    //On the WiiU, it seems Unity has a bug and never calls OnApplicationFocus(true). This leaves us in "suspended mode". So commented out for now.
    //On iOS, application interruptions are handled in the sound engine already.
    void OnApplicationPause(bool pauseStatus)
    {
    if (ms_Instance != null)
    {
    if ( !pauseStatus )
    {
    AkSoundEngine.WakeupFromSuspend();
    }
    else
    {
    AkSoundEngine.Suspend();
    }
    AkSoundEngine.RenderAudio();
    }
    }
    void OnApplicationFocus(bool focus)
    {
    if (ms_Instance != null)
    {
    if ( focus )
    {
    AkSoundEngine.WakeupFromSuspend();
    }
    else
    {
    AkSoundEngine.Suspend();
    }
    AkSoundEngine.RenderAudio();
    }
    }
    #endif*/

    Hope this helps
     
    SmallTimer likes this.
  4. altschuler

    altschuler

    Joined:
    Nov 17, 2017
    Posts:
    7
    Has anyone found a solution to this?
     
  5. altschuler

    altschuler

    Joined:
    Nov 17, 2017
    Posts:
    7
    There is a setting in AkInitializationSettings called Render During Focus Loss that you can set to true (). However, as this does not seem to work (note you need to enter and exit play mode for the changes to take effect), I found a hack around this:

    In AkSoundEngineController.cs (line 186, YMMV) I wrapped the ActivateAudio in an if like this:

    Code (CSharp):
    1.     public void OnApplicationFocus(bool focus)
    2.     {
    3.         // originally just this line: ActivateAudio(focus, AkWwiseInitializationSettings.ActivePlatformSettings.RenderDuringFocusLoss);
    4.         if (Debug.isDebugBuild) {
    5.             ActivateAudio(focus, AkWwiseInitializationSettings.ActivePlatformSettings.RenderDuringFocusLoss);
    6.         }
    7.     }
    8.  
     
    prodteam likes this.