简体   繁体   中英

Detecting if user is playing music from another app

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!

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM