Removed the fade in/out on play/pause option because it caused bugs with the audiofocus and complicated the development. -> Fixed the audiofocus problem where the music wont start playing again when gaining focus back.

This commit is contained in:
Karim Abou Zeid 2015-06-28 23:18:04 +02:00
commit 593211bd2b
6 changed files with 30 additions and 106 deletions

View file

@ -59,7 +59,7 @@ public class MusicPlayerRemote {
public static void pauseSong() { public static void pauseSong() {
if (musicService != null) { if (musicService != null) {
musicService.pause(false); musicService.pause();
} }
} }
@ -82,12 +82,12 @@ public class MusicPlayerRemote {
} }
public static boolean isPlaying() { public static boolean isPlaying() {
return musicService != null && musicService.isPlayingAndNotFadingDown(); return musicService != null && musicService.isPlaying();
} }
public static void resumePlaying() { public static void resumePlaying() {
if (musicService != null) { if (musicService != null) {
musicService.play(false); musicService.play();
} }
} }

View file

@ -90,7 +90,7 @@ public class PlayingNotificationHelper {
} }
this.isColored = isColored; this.isColored = isColored;
currentSong = song; currentSong = song;
this.isPlaying = service.isPlayingAndNotFadingDown(); this.isPlaying = service.isPlaying();
if (!isReceiverRegistered) if (!isReceiverRegistered)
service.registerReceiver(notificationColorPreferenceChangedReceiver, intentFilter); service.registerReceiver(notificationColorPreferenceChangedReceiver, intentFilter);
isReceiverRegistered = true; isReceiverRegistered = true;

View file

@ -79,8 +79,6 @@ public class MusicService extends Service {
private static final int FOCUS_CHANGE = 5; private static final int FOCUS_CHANGE = 5;
private static final int DUCK = 6; private static final int DUCK = 6;
private static final int UNDUCK = 7; private static final int UNDUCK = 7;
private static final int FADE_DOWN_AND_PAUSE = 8;
private static final int FADE_UP_AND_RESUME = 9;
public static final int RELEASE_WAKELOCK = 10; public static final int RELEASE_WAKELOCK = 10;
public static final int TRACK_ENDED = 11; public static final int TRACK_ENDED = 11;
public static final int TRACK_WENT_TO_NEXT = 12; public static final int TRACK_WENT_TO_NEXT = 12;
@ -111,7 +109,6 @@ public class MusicService extends Service {
private PowerManager.WakeLock wakeLock; private PowerManager.WakeLock wakeLock;
private MusicPlayerHandler playerHandler; private MusicPlayerHandler playerHandler;
private QueueSaveHandler queueSaveHandler; private QueueSaveHandler queueSaveHandler;
private boolean isFadingDown = false;
private HandlerThread musicPlayerHandlerThread; private HandlerThread musicPlayerHandlerThread;
private HandlerThread queueSaveHandlerThread; private HandlerThread queueSaveHandlerThread;
private RecentlyPlayedStore recentlyPlayedStore; private RecentlyPlayedStore recentlyPlayedStore;
@ -122,8 +119,7 @@ public class MusicService extends Service {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { if (intent.getAction().equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
pause(true); pause();
pause(false);
} }
} }
}; };
@ -232,17 +228,17 @@ public class MusicService extends Service {
String action = intent.getAction(); String action = intent.getAction();
switch (action) { switch (action) {
case ACTION_TOGGLE_PLAYBACK: case ACTION_TOGGLE_PLAYBACK:
if (isPlayingAndNotFadingDown()) { if (isPlaying()) {
pause(false); pause();
} else { } else {
play(false); play();
} }
break; break;
case ACTION_PAUSE: case ACTION_PAUSE:
pause(false); pause();
break; break;
case ACTION_PLAY: case ACTION_PLAY:
play(false); play();
break; break;
case ACTION_REWIND: case ACTION_REWIND:
back(true); back(true);
@ -319,8 +315,8 @@ public class MusicService extends Service {
getAudioManager().abandonAudioFocus(audioFocusListener); getAudioManager().abandonAudioFocus(audioFocusListener);
} }
public boolean isPlayingAndNotFadingDown() { public boolean isPlaying() {
return player.isPlaying() && !isFadingDown; return player.isPlaying();
} }
public void saveState() { public void saveState() {
@ -471,7 +467,7 @@ public class MusicService extends Service {
} }
private void updateWidgets() { private void updateWidgets() {
MusicPlayerWidget.updateWidgets(this, getCurrentSong(), isPlayingAndNotFadingDown()); MusicPlayerWidget.updateWidgets(this, getCurrentSong(), isPlaying());
} }
private static String getTrackUri(Song song) { private static String getTrackUri(Song song) {
@ -634,46 +630,22 @@ public class MusicService extends Service {
private void playSongAtImpl(int position) { private void playSongAtImpl(int position) {
if (openTrackAndPrepareNextAt(position)) { if (openTrackAndPrepareNextAt(position)) {
play(false); play();
} else { } else {
Toast.makeText(this, getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show(); Toast.makeText(this, getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show();
} }
} }
public void pause(boolean forceNoFading) { public void pause() {
pausedByTransientLossOfFocus = false; pausedByTransientLossOfFocus = false;
if (!forceNoFading && PreferenceUtils.getInstance(this).fadePlayPause()) {
playerHandler.removeMessages(FADE_UP_AND_RESUME);
playerHandler.sendEmptyMessage(FADE_DOWN_AND_PAUSE);
} else {
pauseImpl();
}
}
private void pauseImpl() {
playerHandler.removeMessages(FADE_UP_AND_RESUME);
if (player.isPlaying()) { if (player.isPlaying()) {
player.pause(); player.pause();
notifyChange(PLAY_STATE_CHANGED); notifyChange(PLAY_STATE_CHANGED);
} }
} }
public void play(boolean forceNoFading) { public void play() {
if (!forceNoFading && PreferenceUtils.getInstance(this).fadePlayPause()) {
playerHandler.removeMessages(FADE_DOWN_AND_PAUSE);
playerHandler.sendEmptyMessage(FADE_UP_AND_RESUME);
} else {
playImpl();
try {
player.setVolume(1f);
} catch (IllegalStateException ignored) {
}
}
}
private void playImpl() {
synchronized (this) { synchronized (this) {
playerHandler.removeMessages(FADE_DOWN_AND_PAUSE);
if (requestFocus()) { if (requestFocus()) {
if (!player.isPlaying()) { if (!player.isPlaying()) {
if (!player.isInitialized()) { if (!player.isInitialized()) {
@ -809,7 +781,7 @@ public class MusicService extends Service {
internalIntent.putExtra("album", currentSong.albumName); internalIntent.putExtra("album", currentSong.albumName);
internalIntent.putExtra("track", currentSong.title); internalIntent.putExtra("track", currentSong.title);
} }
internalIntent.putExtra("playing", isPlayingAndNotFadingDown()); internalIntent.putExtra("playing", isPlaying());
sendStickyBroadcast(internalIntent); sendStickyBroadcast(internalIntent);
//to let other apps know whats playing. i.E. last.fm (scrobbling) or musixmatch //to let other apps know whats playing. i.E. last.fm (scrobbling) or musixmatch
@ -818,7 +790,7 @@ public class MusicService extends Service {
sendStickyBroadcast(publicMusicIntent); sendStickyBroadcast(publicMusicIntent);
if (what.equals(PLAY_STATE_CHANGED)) { if (what.equals(PLAY_STATE_CHANGED)) {
final boolean isPlaying = isPlayingAndNotFadingDown(); final boolean isPlaying = isPlaying();
playingNotificationHelper.updatePlayState(isPlaying); playingNotificationHelper.updatePlayState(isPlaying);
MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying); MusicPlayerWidget.updateWidgetsPlayState(this, isPlaying);
//noinspection deprecation //noinspection deprecation
@ -887,7 +859,6 @@ public class MusicService extends Service {
private static final class MusicPlayerHandler extends Handler { private static final class MusicPlayerHandler extends Handler {
private final WeakReference<MusicService> mService; private final WeakReference<MusicService> mService;
private float currentDuckVolume = 1.0f; private float currentDuckVolume = 1.0f;
private float currentPlayPauseFadeVolume = 1.0f;
public MusicPlayerHandler(final MusicService service, final Looper looper) { public MusicPlayerHandler(final MusicService service, final Looper looper) {
super(looper); super(looper);
@ -913,7 +884,7 @@ public class MusicService extends Service {
break; break;
case UNDUCK: case UNDUCK:
currentDuckVolume += .05f; currentDuckVolume += .03f;
if (currentDuckVolume < 1.0f) { if (currentDuckVolume < 1.0f) {
sendEmptyMessageDelayed(UNDUCK, 10); sendEmptyMessageDelayed(UNDUCK, 10);
} else { } else {
@ -922,43 +893,9 @@ public class MusicService extends Service {
service.player.setVolume(currentDuckVolume); service.player.setVolume(currentDuckVolume);
break; break;
case FADE_DOWN_AND_PAUSE:
if (!service.isFadingDown) {
service.isFadingDown = true;
service.notifyChange(PLAY_STATE_CHANGED);
}
currentPlayPauseFadeVolume -= .125f;
if (currentPlayPauseFadeVolume > 0f) {
sendEmptyMessageDelayed(FADE_DOWN_AND_PAUSE, 10);
} else {
currentPlayPauseFadeVolume = 0f;
service.isFadingDown = false;
service.pause(true);
}
service.player.setVolume(currentPlayPauseFadeVolume);
break;
case FADE_UP_AND_RESUME:
if (service.isFadingDown) {
service.isFadingDown = false;
service.notifyChange(PLAY_STATE_CHANGED);
}
service.playImpl();
currentPlayPauseFadeVolume += .125f;
if (currentPlayPauseFadeVolume < 1.0f) {
sendEmptyMessageDelayed(FADE_UP_AND_RESUME, 10);
} else {
currentPlayPauseFadeVolume = 1.0f;
}
try {
service.player.setVolume(currentPlayPauseFadeVolume);
} catch (IllegalStateException ignored) {
}
break;
case TRACK_WENT_TO_NEXT: case TRACK_WENT_TO_NEXT:
if (service.getRepeatMode() == REPEAT_MODE_NONE && service.isLastTrack()) { if (service.getRepeatMode() == REPEAT_MODE_NONE && service.isLastTrack()) {
service.pause(true); service.pause();
service.seek(0); service.seek(0);
} else { } else {
service.setPosition(service.nextPosition); service.setPosition(service.nextPosition);
@ -988,8 +925,9 @@ public class MusicService extends Service {
switch (msg.arg1) { switch (msg.arg1) {
case AudioManager.AUDIOFOCUS_GAIN: case AudioManager.AUDIOFOCUS_GAIN:
service.registerReceiversAndRemoteControlClient(); service.registerReceiversAndRemoteControlClient();
if (!service.isPlayingAndNotFadingDown() && service.pausedByTransientLossOfFocus) { if (!service.isPlaying() && service.pausedByTransientLossOfFocus) {
service.play(false); service.play();
service.pausedByTransientLossOfFocus = false;
} }
removeMessages(DUCK); removeMessages(DUCK);
sendEmptyMessage(UNDUCK); sendEmptyMessage(UNDUCK);
@ -997,7 +935,7 @@ public class MusicService extends Service {
case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for an unbounded amount of time: stop playback and release media player // Lost focus for an unbounded amount of time: stop playback and release media player
service.pause(true); service.pause();
service.unregisterReceiversAndRemoteControlClient(); service.unregisterReceiversAndRemoteControlClient();
break; break;
@ -1005,8 +943,9 @@ public class MusicService extends Service {
// Lost focus for a short time, but we have to stop // Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback // playback. We don't release the media player because playback
// is likely to resume // is likely to resume
service.pause(false); boolean wasPlaying = service.isPlaying();
service.pausedByTransientLossOfFocus = service.isPlayingAndNotFadingDown(); service.pause();
service.pausedByTransientLossOfFocus = wasPlaying;
break; break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:

View file

@ -40,7 +40,6 @@ public final class PreferenceUtils {
public static final String LARGER_TITLE_BOX_NOW_PLAYING = "larger_title_box_now_playing"; public static final String LARGER_TITLE_BOX_NOW_PLAYING = "larger_title_box_now_playing";
public static final String ALTERNATIVE_PROGRESS_SLIDER_NOW_PLAYING = "alternative_progress_slider_now_playing"; public static final String ALTERNATIVE_PROGRESS_SLIDER_NOW_PLAYING = "alternative_progress_slider_now_playing";
public static final String PLAYBACK_CONTROLLER_CARD_NOW_PLAYING = "playback_controller_card_now_playing"; public static final String PLAYBACK_CONTROLLER_CARD_NOW_PLAYING = "playback_controller_card_now_playing";
public static final String FADE_PLAY_PAUSE = "fade_play_pause";
public static final String COLORED_NOTIFICATION = "colored_notification"; public static final String COLORED_NOTIFICATION = "colored_notification";
public static final String GAPLESS_PLAYBACK = "gapless_playback"; public static final String GAPLESS_PLAYBACK = "gapless_playback";
public static final String LAST_ADDED_CUTOFF_TIMESTAMP = "last_added_cutoff_timestamp"; public static final String LAST_ADDED_CUTOFF_TIMESTAMP = "last_added_cutoff_timestamp";
@ -196,10 +195,6 @@ public final class PreferenceUtils {
return mPreferences.getBoolean(ALTERNATIVE_PROGRESS_SLIDER_NOW_PLAYING, false); return mPreferences.getBoolean(ALTERNATIVE_PROGRESS_SLIDER_NOW_PLAYING, false);
} }
public final boolean fadePlayPause() {
return mPreferences.getBoolean(FADE_PLAY_PAUSE, true);
}
public final boolean gaplessPlayback() { public final boolean gaplessPlayback() {
return mPreferences.getBoolean(GAPLESS_PLAYBACK, false); return mPreferences.getBoolean(GAPLESS_PLAYBACK, false);
} }

View file

@ -115,7 +115,6 @@
<string name="pref_title_colored_album_footers">Colored Album Footers</string> <string name="pref_title_colored_album_footers">Colored Album Footers</string>
<string name="pref_title_album_art_on_lockscreen">Album Art on Lockscreen</string> <string name="pref_title_album_art_on_lockscreen">Album Art on Lockscreen</string>
<string name="pref_title_colored_notification">Colored Notification</string> <string name="pref_title_colored_notification">Colored Notification</string>
<string name="pref_title_fade_play_pause">Fade Play/Pause</string>
<string name="pref_title_gapless_playback">Gapless Playback</string> <string name="pref_title_gapless_playback">Gapless Playback</string>
<string name="pref_title_force_square_album_art">Force Square Album Cover</string> <string name="pref_title_force_square_album_art">Force Square Album Cover</string>
<string name="pref_title_opaque_toolbar_now_playing">Opaque Toolbar</string> <string name="pref_title_opaque_toolbar_now_playing">Opaque Toolbar</string>
@ -152,7 +151,6 @@
<string name="pref_summary_colored_album_footers">"Album footers in the grid are colored with the album cover\'s vibrant color."</string> <string name="pref_summary_colored_album_footers">"Album footers in the grid are colored with the album cover\'s vibrant color."</string>
<string name="pref_summary_album_art_on_lockscreen">The album art is shown on the lockscreen. You might have to restart Phonograph in order for changes to take affect.</string> <string name="pref_summary_album_art_on_lockscreen">The album art is shown on the lockscreen. You might have to restart Phonograph in order for changes to take affect.</string>
<string name="pref_summary_colored_notification">"The notification is colored with the album cover\'s vibrant color."</string> <string name="pref_summary_colored_notification">"The notification is colored with the album cover\'s vibrant color."</string>
<string name="pref_summary_fade_play_pause">"Fades the song in/out on play/pause."</string>
<string name="pref_summary_gapless_playback">"Eliminates the gap between two songs. This can cause playback issues on some devices."</string> <string name="pref_summary_gapless_playback">"Eliminates the gap between two songs. This can cause playback issues on some devices."</string>
<string name="pref_summary_force_square_album_art">Album art in the now playing view is forced to be squared.</string> <string name="pref_summary_force_square_album_art">Album art in the now playing view is forced to be squared.</string>
<string name="pref_summary_opaque_toolbar_now_playing">The toolbar is opaque and does not cover the album art.</string> <string name="pref_summary_opaque_toolbar_now_playing">The toolbar is opaque and does not cover the album art.</string>

View file

@ -4,24 +4,16 @@
<com.kabouzeid.gramophone.prefs.DynamicPreferenceCategory android:title="@string/pref_header_audio"> <com.kabouzeid.gramophone.prefs.DynamicPreferenceCategory android:title="@string/pref_header_audio">
<CheckBoxPreference <CheckBoxPreference
android:layout="@layout/preference_custom"
android:defaultValue="true"
android:key="fade_play_pause"
android:title="@string/pref_title_fade_play_pause"
android:summary="@string/pref_summary_fade_play_pause"
android:widgetLayout="@layout/preference_dynamic_checkbox" />
<CheckBoxPreference
android:layout="@layout/preference_custom"
android:defaultValue="false" android:defaultValue="false"
android:key="gapless_playback" android:key="gapless_playback"
android:title="@string/pref_title_gapless_playback" android:layout="@layout/preference_custom"
android:summary="@string/pref_summary_gapless_playback" android:summary="@string/pref_summary_gapless_playback"
android:title="@string/pref_title_gapless_playback"
android:widgetLayout="@layout/preference_dynamic_checkbox" /> android:widgetLayout="@layout/preference_dynamic_checkbox" />
<Preference <Preference
android:layout="@layout/preference_custom"
android:key="equalizer" android:key="equalizer"
android:layout="@layout/preference_custom"
android:title="@string/equalizer" /> android:title="@string/equalizer" />
</com.kabouzeid.gramophone.prefs.DynamicPreferenceCategory> </com.kabouzeid.gramophone.prefs.DynamicPreferenceCategory>