Handle bluetooth and headphone disconnect (#4722)

This commit is contained in:
Tony Tam 2021-07-11 10:36:02 -07:00 committed by GitHub
parent 07cd141bdf
commit e8eb245899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View File

@ -1485,7 +1485,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
Log.d(TAG, "Received Auto Connection update: " + status); Log.d(TAG, "Received Auto Connection update: " + status);
if (!isConnectedToCar) { if (!isConnectedToCar) {
Log.d(TAG, "Car was unplugged during playback."); Log.d(TAG, "Car was unplugged during playback.");
pauseIfPauseOnDisconnect();
} else { } else {
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus(); PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) { if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) {
@ -1519,11 +1518,10 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (TextUtils.equals(intent.getAction(), Intent.ACTION_HEADSET_PLUG)) { if (TextUtils.equals(intent.getAction(), Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1); int state = intent.getIntExtra("state", -1);
Log.d(TAG, "Headset plug event. State is " + state);
if (state != -1) { if (state != -1) {
Log.d(TAG, "Headset plug event. State is " + state);
if (state == UNPLUGGED) { if (state == UNPLUGGED) {
Log.d(TAG, "Headset was unplugged during playback."); Log.d(TAG, "Headset was unplugged during playback.");
pauseIfPauseOnDisconnect();
} else if (state == PLUGGED) { } else if (state == PLUGGED) {
Log.d(TAG, "Headset was plugged in during playback."); Log.d(TAG, "Headset was plugged in during playback.");
unpauseIfPauseOnDisconnect(false); unpauseIfPauseOnDisconnect(false);
@ -1556,18 +1554,16 @@ public class PlaybackService extends MediaBrowserServiceCompat {
Log.d(TAG, "Pausing playback because audio is becoming noisy"); Log.d(TAG, "Pausing playback because audio is becoming noisy");
pauseIfPauseOnDisconnect(); pauseIfPauseOnDisconnect();
} }
// android.media.AUDIO_BECOMING_NOISY
}; };
/** /**
* Pauses playback if PREF_PAUSE_ON_HEADSET_DISCONNECT was set to true. * Pauses playback if PREF_PAUSE_ON_HEADSET_DISCONNECT was set to true.
*/ */
private void pauseIfPauseOnDisconnect() { private void pauseIfPauseOnDisconnect() {
Log.d(TAG, "pauseIfPauseOnDisconnect()");
if (UserPreferences.isPauseOnHeadsetDisconnect() && !isCasting()) { if (UserPreferences.isPauseOnHeadsetDisconnect() && !isCasting()) {
if (mediaPlayer.getPlayerStatus() == PlayerStatus.PLAYING) { transientPause = true;
transientPause = true; mediaPlayer.pause(!UserPreferences.isPersistNotify(), false);
}
mediaPlayer.pause(!UserPreferences.isPersistNotify(), true);
} }
} }
@ -1575,6 +1571,10 @@ public class PlaybackService extends MediaBrowserServiceCompat {
* @param bluetooth true if the event for unpausing came from bluetooth * @param bluetooth true if the event for unpausing came from bluetooth
*/ */
private void unpauseIfPauseOnDisconnect(boolean bluetooth) { private void unpauseIfPauseOnDisconnect(boolean bluetooth) {
if (mediaPlayer.isAudioChannelInUse()) {
Log.d(TAG, "unpauseIfPauseOnDisconnect() audio is in use");
return;
}
if (transientPause) { if (transientPause) {
transientPause = false; transientPause = false;
if (!bluetooth && UserPreferences.isUnpauseOnHeadsetReconnect()) { if (!bluetooth && UserPreferences.isUnpauseOnHeadsetReconnect()) {

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.service.playback; package de.danoeh.antennapod.core.service.playback;
import android.content.Context; import android.content.Context;
import android.media.AudioManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
@ -330,6 +331,11 @@ public abstract class PlaybackServiceMediaPlayer {
callback.statusChanged(new PSMPInfo(oldPlayerStatus, playerStatus, getPlayable())); callback.statusChanged(new PSMPInfo(oldPlayerStatus, playerStatus, getPlayable()));
} }
public boolean isAudioChannelInUse() {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
return (audioManager.getMode() != AudioManager.MODE_NORMAL || audioManager.isMusicActive());
}
/** /**
* @see #setPlayerStatus(PlayerStatus, Playable, int) * @see #setPlayerStatus(PlayerStatus, Playable, int)
*/ */