Music Player App - 避免两个音乐播放器同时播放
Music Player App - avoid that two music players play at the same time
我正在使用 ExoPlayer 开发歌曲播放器应用程序。
我目前遇到的问题是,在 Android 7.x 设备上,用户可以同时收听两个播放器。这似乎是新 Android 版本的事情。
我想知道是否可以避免这种行为,使用户在我的应用程序上收听媒体时无法收听两个播放器。
您可能需要在应用程序中管理音频焦点。
你可以在这里看看:
https://developer.android.com/guide/topics/media-apps/audio-focus.html
To avoid every music app playing at the same time, Android introduces
the idea of audio focus. Only one app can hold audio focus at a time.
When your app needs to output audio, it should request audio focus.
AudioManager mAudioManager_ = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener_ = new AudioManager.OnAudioFocusChangeListener()
{ @Override
public void onAudioFocusChange (int focusChange)
{ switch (focusChange)
{ case AudioManager.AUDIOFOCUS_GAIN:
Log.e("DEBUG", "##### AUDIOFOCUS_GAIN");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e("DEBUG", "##### AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e("DEBUG", "##### AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.e("DEBUG", "##### AUDIOFOCUS_LOSS");
break;
}
}
};
if (mAudioManager_.requestAudioFocus(mOnAudioFocusChangeListener_, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{ // start play
// ...
mAudioManager_.abandonAudioFocus(mOnAudioFocusChangeListener_);
}
我正在使用 ExoPlayer 开发歌曲播放器应用程序。 我目前遇到的问题是,在 Android 7.x 设备上,用户可以同时收听两个播放器。这似乎是新 Android 版本的事情。
我想知道是否可以避免这种行为,使用户在我的应用程序上收听媒体时无法收听两个播放器。
您可能需要在应用程序中管理音频焦点。 你可以在这里看看: https://developer.android.com/guide/topics/media-apps/audio-focus.html
To avoid every music app playing at the same time, Android introduces the idea of audio focus. Only one app can hold audio focus at a time.
When your app needs to output audio, it should request audio focus.
AudioManager mAudioManager_ = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener_ = new AudioManager.OnAudioFocusChangeListener()
{ @Override
public void onAudioFocusChange (int focusChange)
{ switch (focusChange)
{ case AudioManager.AUDIOFOCUS_GAIN:
Log.e("DEBUG", "##### AUDIOFOCUS_GAIN");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e("DEBUG", "##### AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e("DEBUG", "##### AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.e("DEBUG", "##### AUDIOFOCUS_LOSS");
break;
}
}
};
if (mAudioManager_.requestAudioFocus(mOnAudioFocusChangeListener_, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{ // start play
// ...
mAudioManager_.abandonAudioFocus(mOnAudioFocusChangeListener_);
}