Search Unity

How to Manage Audio Focus on Android using Unity - [Solved]

Discussion in 'Android' started by MWB, Apr 23, 2016.

  1. MWB

    MWB

    Joined:
    Mar 5, 2014
    Posts:
    2
    Long story short:
    What is the simplest way to set up appropriate Audio Focus behavior?

    I don't need to do anything complicated, only have it properly request Audio Focus on app load as I'm having trouble getting it past verification without it.

    Short story long:
    I need to manage Audio Focus behavior on my app, however as far as I've understood it isn't something that is easily done from within Unity itself.

    I have been linked to this documentation: http://developer.android.com/training/managing-audio/audio-focus.html which I imagine would have been very useful if I had any experience with Android Studio, which I don't... I understand that this is something I'll have to look into, but I figured I'd try my luck here for a bit of a helping hand.

    My current "theory" of what I need to do is this: Create some sort of .jav plug-in in Android Studio which configures the Audio Focus behavior and then load that into my Unity project somehow. Is that where I should be focusing my time or have I misunderstood something? Just knowing that I'm on the right path would be highly appreciated, even if you don't have the time to explain it in-depth.

    Most of this is based on my vague understanding of the topic, so any corrections would be greatly appreciated. Bonus points for linking me to any appropriate documentation that I need to study.

    Thanks,
    Mike
     
  2. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    524
    What version of Unity are you using? Unity 5.4 will grab audio focus on startup and also react to focus changes. The fix was supposed to be back ported to 5.3 but wasn't :-(

    That said it should be fairly easy to write a C# script that fix this. No need to write any plugin. Something like:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System;
    4. using System.Collections;
    5.  
    6. public class DeviceAudioListener : MonoBehaviour
    7. {
    8.     bool m_ListenerRegistered = false;
    9.     AudioFocusListener m_FocusListener;
    10.  
    11.     class AudioFocusListener : AndroidJavaProxy
    12.     {
    13.         public AudioFocusListener() : base("android.media.AudioManager$OnAudioFocusChangeListener") { }
    14.  
    15.         public bool m_HasAudioFocus = true;      
    16.  
    17.         public bool HasAudioFocus { get { return m_HasAudioFocus;} }
    18.  
    19.         public void onAudioFocusChange(int focus)
    20.         {
    21.             m_HasAudioFocus = (focus >= 0);
    22.         }
    23.  
    24.         public string toString()
    25.         {
    26.             return "MyAwesomeAudioListener";
    27.         }
    28.     }
    29.  
    30.     void Awake()
    31.     {
    32.         if (m_FocusListener == null)
    33.             m_FocusListener = new AudioFocusListener();      
    34.  
    35.         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    36.         AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    37.         AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
    38.  
    39.            audioManager.Call<Int32>("requestAudioFocus", m_FocusListener, 3, 1);
    40.     }
    41.  
    42.     void FixedUpdate()
    43.     {
    44.         AudioListener.pause = !m_FocusListener.HasAudioFocus;
    45.     }
    46. }
     
  3. MWB

    MWB

    Joined:
    Mar 5, 2014
    Posts:
    2
    Perfect! That worked like a charm, at least in my preliminary testing. I'll spend some time going through that code to understand it properly.

    For future reference: On this project I was on 5.2.4f1 for... reasons. I am looking to upgrade to 5.3.x for this specific project, but hearing that Audio Focus is automatic in 5.4 will probably be very useful for a lot of people including my own future projects.

    I am very grateful for your help, it seems I was entirely over-complicating things and you saved me a world of worry.

    I'll make sure to post my work in the Made in Unity section once I get some additional things sorted.
     
    Last edited: Apr 24, 2016
  4. Deleted User

    Deleted User

    Guest

    Solution provided by bitter not working anymore (at least in Unity 5.3.5f1 and 5.3.5p2), after calling:
    audioManager.Call<Int32>("requestAudioFocus", m_FocusListener, 3, 1);
    (call ends with AUDIOFOCUS_REQUEST_GRANTED) sounds stop working at all, at least until audioManager.Call<Int32>("abandonAudioFocus", m_FocusListener)
    is called.

    Guess OnAudioFocusChangeListener is now implemented internally in Unity engine, too bad its not doing his job properly, because after SMS recive or phone call, sounds never returning to app. So instead solution of problem, situation is now even worse... ;).



     
    Last edited by a moderator: Jun 14, 2016
  5. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    524
    Yeah, sorry about that. We have a fix on the way..
     
  6. jlastra

    jlastra

    Joined:
    Apr 1, 2015
    Posts:
    2
    We are using 5.3.4p6 and I can confirm that the behavior reported by @tomekteyon (no audio at all if calilng "requestAudioFocus") also happens to us.
    Is this fixed in 5.3.5p3? I don't see a line about it on the change log. If not , which version should we roll back to be able to implemente the "requestAudioFocus" as suggested some posts above?
    We are about to publish our game in the following two weeks, so having a version in which Audio Focus is correctly handled (either by Unity or by us) is super urgent for us.
    Thanks!
     
  7. robson_depaula

    robson_depaula

    Joined:
    Sep 30, 2014
    Posts:
    37
    Hello,

    Was on the same boat (release and audio focus issue with 5.3.5x).

    An workaround for me was to build with 5.3.4f1, the audiofocus is working fine.
     
  8. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    524
    The fix has been committed for the soon to be released 5.3.5p4
     
  9. BRiSA

    BRiSA

    Joined:
    Mar 16, 2015
    Posts:
    4
    Hi,

    Just wandering, is there a way to disable audio focus?
    Seems like Unity internally controls audio focus and I want users to listen to their own music while playing my game.
    I don't know how I can disable this.

    PS I am using Unity 5.3.5f1
     
  10. bruce-wu

    bruce-wu

    Joined:
    Feb 1, 2014
    Posts:
    1
    Hi,

    Using 5.3.5p4, while the patch fixed the issue of other application took over audiofocus / back (like opening youtube to play video, then back)... incoming phone calls still break game audio, and the only way to get it back is to restart the game.
     
  11. OsamaDeep

    OsamaDeep

    Joined:
    Jun 22, 2013
    Posts:
    13
    that happens to me too, any notification from outside the game break the audio system, i test the last staible build and the last beta build, same problem. we need to fix this deadly bug.
     
  12. jlastra

    jlastra

    Joined:
    Apr 1, 2015
    Posts:
    2
    Hey, I've not tried 5.3.5p4, but I can confirm that using 5.3.4p4 the audio focus works fine if you manually handle it via JNI or a native activity. So, in the meantime, we are sticking with 5.3.4p4.
     
  13. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    524
    I'm sorry, I @#$ up. A new fix will be available in 5.3.5p7.
     
  14. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    148
    I just don't want to stop system background music at startup, will this patch allow me to do this ?
     
  15. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    148
    Can I prevent unity to request audiofocus at startup ?
     
  16. Deathfate

    Deathfate

    Joined:
    Sep 10, 2012
    Posts:
    46
    I would love to know how to prevent the request of audiofocus
     
  17. okuligowski

    okuligowski

    Joined:
    Apr 27, 2013
    Posts:
    2
  18. randydots

    randydots

    Joined:
    Dec 27, 2014
    Posts:
    6
    Do you find any method to do so?
     
  19. randydots

    randydots

    Joined:
    Dec 27, 2014
    Posts:
    6
    Do you have any solution to solve this. I am facing the same issue now.
     
  20. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    148
    Disable unity audio...
     
  21. lloydg

    lloydg

    Joined:
    Mar 23, 2015
    Posts:
    36
    I want to be able to disable the audio focus but still have my audio enabled. I just have small sound effects every now and then, I want my users to be able to listen to there music and also hear my sound effects.
     
  22. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    148
  23. lloydg

    lloydg

    Joined:
    Mar 23, 2015
    Posts:
    36
    Yeah I already put all 10 votes into that, its currently blocking me making a release. :(
     
  24. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    148
    You certainly will be block for a very long time if you're waiting for change about this
     
  25. lloydg

    lloydg

    Joined:
    Mar 23, 2015
    Posts:
    36
    Yeah, I have hacked it to work for Android (disabled sounds in unity and using a native plugin to play all sounds.)

    I am hoping this wont also be an issue on iOS
     
  26. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    215
  27. asotelo94

    asotelo94

    Joined:
    Jan 26, 2013
    Posts:
    12
    Is there an update on this? I am using unity 2018.2 and can't play a music app while inside the game. No audio-focus management.
     
  28. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    462
    Make sure "Mute Other Audio Sources" is not enabled in the player settings.
     
  29. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    147
    I tried this and the onAudioFocusChange gets called when external music starts (as expected) but when I then stop the external music the onAudioFocusChange does not get called again, so I don't know when to resume my game music. Is there something else I need to do?
     
unityunity