2

My game has an in-game soundtrack and I would like to pause the music if the user is playing music of their own from a media app on Android or iOS.

Is there a way to do this that is efficient enough to run in an update() function?

Thanks in advance!

Community
  • 1
  • 1
Tiaan
  • 698
  • 2
  • 10
  • 32

2 Answers2

4

You can check if music is playing as described from this post.

AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
 {
     // do something - or do it not
 }

Wrap it around a class in Java then call it from C# with the help of the AndroidJavaClass API.


But that requires Java. You can take that code and convert it to C# without Java at-all. Get UnityPlayerPlayer, Activity then the Context. The rest can be handled by AndroidJavaClass.

Here is the ported C# version that does not require Java plugin.

bool isMusicPlaying()
{
    const string AUDIO_SERVICE = "audio";
    AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

    bool mIsPlaying;
    using (AndroidJavaObject audioManager = unityContext.Call<AndroidJavaObject>("getSystemService", AUDIO_SERVICE))
    {
        mIsPlaying = audioManager.Call<bool>("isMusicActive");
    }
    return mIsPlaying;
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Hi, that code is supposed to work. I did a bit research about this and found out that this is a bug from Unity. See [this](https://answers.unity3d.com/questions/984710/detect-if-music-is-playing-in-other-apps-on-androi.html) and [this](https://issuetracker.unity3d.com/issues/android-native-android-method-audiomanager-dot-ismusicactive-always-returns-true). File for a bug report. – Programmer Jun 05 '17 at 00:09
2

You have to implements the callback onAudioFocusChange() that your app will receive when some other app acquires or abandons audio focus.

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
                         // Use the music stream.
                         AudioManager.STREAM_MUSIC,
                         // Request permanent focus.
                         AudioManager.AUDIOFOCUS_GAIN);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    // Start playback
}

And the callback itself:

AudioManager.OnAudioFocusChangeListener afChangeListener = new AudioManager.OnAudioFocusChangeListener() {

public void onAudioFocusChange(int focusChange) {
  if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
    // Permanent loss of audio focus
    // Pause playback immediately
  } else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
    // Pause playback
  } else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
    // Lower the volume, keep playing
  } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
    // Your app has been granted audio focus again
    // Raise volume to normal, restart playback if necessary
  }
}

Take a look on this official guide: Handling Changes in Audio Output

Alex Kamenkov
  • 891
  • 1
  • 6
  • 16