Search Unity

Bug Enable Custom Background Behaviors - NOT working (at least on iOS)

Discussion in 'Audio & Video' started by Kobald-Klaus, Oct 18, 2020.

  1. Kobald-Klaus

    Kobald-Klaus

    Joined:
    Jun 20, 2014
    Posts:
    127
    I am creating an audio player, so audio has to continue playing when player is in background.
    But it does not work on iOS. (don´t know about Android yet)

    I have selected "Audio, Airplay, PiP" in Player Settings
    I have
    Application.runInBackground = true;
    in my script.

    I am starting audioclips (1-3 minutes) via audiosource.Start()
    Audio fades away after a few seconds in background and continues when app gets focused.
     
    Last edited: Oct 18, 2020
  2. Kobald-Klaus

    Kobald-Klaus

    Joined:
    Jun 20, 2014
    Posts:
    127
    Answering my own thread!

    I had to put these lines in the

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    into the xcode project
    - (void)startUnity:(UIApplication*)application


    @Unity GUYS!
    Why can´t YOU put that there? Somebody came up with the solution 5 years ago!!!
     
  3. barraxworld

    barraxworld

    Joined:
    Nov 22, 2015
    Posts:
    5
    Hey Kobald-Klaus,

    Thanks for sharing this information. It helped me on a path to find a solution for playing my Unity Audio while the app is in the background.

    I also wanted to allow my Unity Audio to mix with other audio apps (for instance with a music player like Spotify).

    Here are the additions I added to UnityAppController.mm (Within the startUnity() method)
    I left in the Unity generated code for reference.

    EDIT:
    Two other important steps (which have been mentioned - but I'll say again)....
    1.) Import/include AVFoundation into your build in xCode (that's one I missed as well)
    2.) In Unity PlayerSettings enable custom background modes and select the Audio option.

    Code (CSharp):
    1.  
    2.  
    3.     AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    4.     /*--------------------------------------------------*/
    5.     /* GENERATED BY UNITY - COMMENTING-OUT....
    6.     [audioSession setActive: YES error: nil];
    7.     [audioSession addObserver: self forKeyPath: @"outputVolume" options: 0 context: nil];
    8.     UnityUpdateMuteState([audioSession outputVolume] < 0.01f ? 1 : 0);
    9.     */
    10.     /*--------------------------------------------------*/
    11.     /* THEN ADD IN THE FOLLOWING.... */
    12.     [audioSession setCategory: AVAudioSessionCategoryPlayback
    13.                         withOptions: AVAudioSessionCategoryOptionMixWithOthers
    14.                         error: nil];
    15.     [audioSession setActive: YES error: nil];
    16.     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
     
    Last edited: Dec 18, 2020
  4. BigGameCompany

    BigGameCompany

    Joined:
    Sep 29, 2016
    Posts:
    112
    Do the iOS audio controls work for you using this method?

    Thanks