Fixed support for transient loss of audio focus (e.g. for an incoming

call). Pause the music, then automatically resume when we retain focus.
This commit is contained in:
casey langen 2017-05-21 16:30:15 -07:00
parent 20a19218fb
commit 167d7acacd
3 changed files with 27 additions and 4 deletions

View File

@ -9,8 +9,8 @@ android {
applicationId "io.casey.musikcube.remote"
minSdkVersion 16
targetSdkVersion 25
versionCode 8
versionName "0.7.1"
versionCode 9
versionName "0.7.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

View File

@ -3,7 +3,6 @@ package io.casey.musikcube.remote.playback;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;

View File

@ -52,6 +52,7 @@ public class StreamingPlaybackService implements PlaybackService {
private AudioManager audioManager;
private SharedPreferences prefs;
private int lastSystemVolume;
private boolean pausedByTransientLoss = false;
private Random random = new Random();
private Handler handler = new Handler();
@ -238,6 +239,7 @@ public class StreamingPlaybackService implements PlaybackService {
cancelScheduledPausedShutdown();
context.currentPlayer.resume();
setState(PlaybackState.Playing);
pausedByTransientLoss = false;
}
}
@ -405,6 +407,14 @@ public class StreamingPlaybackService implements PlaybackService {
return this.queryFactory;
}
private void pauseTransient() {
if (state != PlaybackState.Paused) {
pausedByTransientLoss = true;
setState(PlaybackState.Paused);
context.currentPlayer.pause();
}
}
private float getVolumeStep() {
if (prefs.getBoolean(Prefs.Key.SOFTWARE_VOLUME, Prefs.Default.SOFTWARE_VOLUME)) {
return 0.1f;
@ -723,6 +733,7 @@ public class StreamingPlaybackService implements PlaybackService {
cancelScheduledPausedShutdown();
SystemService.wakeup();
this.pausedByTransientLoss = false;
this.context.stopPlaybackAndReset();
final PlaybackContext context = new PlaybackContext();
this.context = context;
@ -865,11 +876,24 @@ public class StreamingPlaybackService implements PlaybackService {
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
PlayerWrapper.unduck();
if (pausedByTransientLoss) {
pausedByTransientLoss = false;
resume();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
killAudioFocus();
pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
switch (getPlaybackState()) {
case Playing:
case Buffering:
pauseTransient();
break;
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: