自动恢复 android 播放背景音乐
Auto resume Background music playing in android
我正在开发一个在后台将音乐作为音频服务运行的音乐播放器。
假设音乐正在后台通过我的应用播放,用户从其他应用(例如:Youtube)播放另一个媒体文件,然后退出该应用。这会暂停通过我的应用程序播放的音乐,现在用户必须手动恢复音乐。
如何在外部媒体文件关闭后自动恢复音乐文件?
我认为您正在寻找控制 Audio Focus 的方法。这讨论了当另一个应用程序/您的应用程序控制音频焦点时如何实现更改,即增益调整(降低音量)。
mAudioManager = (AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
mPlaybackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mPlaybackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(mMyFocusListener, mMyHandler)
.build();
mMediaPlayer = new MediaPlayer();
final Object mFocusLock = new Object();
boolean mPlaybackDelayed = false;
boolean mPlaybackNowAuthorized = false;
// ...
int res = mAudioManager.requestAudioFocus(mFocusRequest);
synchronized(mFocusLock) {
if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
mPlaybackNowAuthorized = false;
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playbackNow();
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
mPlaybackDelayed = true;
mPlaybackNowAuthorized = false;
}
}
// ...
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
if (mPlaybackDelayed || mResumeOnFocusGain) {
synchronized(mFocusLock) {
mPlaybackDelayed = false;
mResumeOnFocusGain = false;
}
playbackNow();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
synchronized(mFocusLock) {
mResumeOnFocusGain = false;
mPlaybackDelayed = false;
}
pausePlayback();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
synchronized(mFocusLock) {
mResumeOnFocusGain = true;
mPlaybackDelayed = false;
}
pausePlayback();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// ... pausing or ducking depends on your app
break;
}
}
}
不过请注意如documentation所述:
Permanent loss of focus
If the audio focus loss is permanent
(AUDIOFOCUS_LOSS), another app is playing audio. Your app should pause
playback immediately, as it won't ever receive an AUDIOFOCUS_GAIN
callback. To restart playback, the user must take an explicit action,
like pressing the play transport control in a notification or app UI.
这是假设接收到永久音频焦点丢失,但是我可以看到一些解决方法,例如轮询音频焦点,但最好不要这样做。如前所述,他们明确表示:
the user must take an explicit action
我正在开发一个在后台将音乐作为音频服务运行的音乐播放器。
假设音乐正在后台通过我的应用播放,用户从其他应用(例如:Youtube)播放另一个媒体文件,然后退出该应用。这会暂停通过我的应用程序播放的音乐,现在用户必须手动恢复音乐。
如何在外部媒体文件关闭后自动恢复音乐文件?
我认为您正在寻找控制 Audio Focus 的方法。这讨论了当另一个应用程序/您的应用程序控制音频焦点时如何实现更改,即增益调整(降低音量)。
mAudioManager = (AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
mPlaybackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mPlaybackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(mMyFocusListener, mMyHandler)
.build();
mMediaPlayer = new MediaPlayer();
final Object mFocusLock = new Object();
boolean mPlaybackDelayed = false;
boolean mPlaybackNowAuthorized = false;
// ...
int res = mAudioManager.requestAudioFocus(mFocusRequest);
synchronized(mFocusLock) {
if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
mPlaybackNowAuthorized = false;
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playbackNow();
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
mPlaybackDelayed = true;
mPlaybackNowAuthorized = false;
}
}
// ...
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
if (mPlaybackDelayed || mResumeOnFocusGain) {
synchronized(mFocusLock) {
mPlaybackDelayed = false;
mResumeOnFocusGain = false;
}
playbackNow();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
synchronized(mFocusLock) {
mResumeOnFocusGain = false;
mPlaybackDelayed = false;
}
pausePlayback();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
synchronized(mFocusLock) {
mResumeOnFocusGain = true;
mPlaybackDelayed = false;
}
pausePlayback();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// ... pausing or ducking depends on your app
break;
}
}
}
不过请注意如documentation所述:
Permanent loss of focus
If the audio focus loss is permanent (AUDIOFOCUS_LOSS), another app is playing audio. Your app should pause playback immediately, as it won't ever receive an AUDIOFOCUS_GAIN callback. To restart playback, the user must take an explicit action, like pressing the play transport control in a notification or app UI.
这是假设接收到永久音频焦点丢失,但是我可以看到一些解决方法,例如轮询音频焦点,但最好不要这样做。如前所述,他们明确表示:
the user must take an explicit action