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.
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:
using UnityEngine;
using System;
using System.Collections;
public class DeviceAudioListener : MonoBehaviour
{
bool m_ListenerRegistered = false;
AudioFocusListener m_FocusListener;
class AudioFocusListener : AndroidJavaProxy
{
public AudioFocusListener() : base("android.media.AudioManager$OnAudioFocusChangeListener") { }
public bool m_HasAudioFocus = true;
public bool HasAudioFocus { get { return m_HasAudioFocus;} }
public void onAudioFocusChange(int focus)
{
m_HasAudioFocus = (focus >= 0);
}
public string toString()
{
return "MyAwesomeAudioListener";
}
}
void Awake()
{
if (m_FocusListener == null)
m_FocusListener = new AudioFocusListener();
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
audioManager.Call<Int32>("requestAudioFocus", m_FocusListener, 3, 1);
}
void FixedUpdate()
{
AudioListener.pause = !m_FocusListener.HasAudioFocus;
}
}
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.
Solution provided by bitter not working anymore (at least in Unity 5.3.5f1 and 5.3.5p2), after calling: audioManager.Call(“requestAudioFocus”, m_FocusListener, 3, 1);
(call ends with AUDIOFOCUS_REQUEST_GRANTED) sounds stop working at all, at least until audioManager.Call(“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… ;).
We are using 5.3.4p6 and I can confirm that the behavior reported by @ (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!
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.
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.
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.
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.