From fbd305cc237f52fb40ccdb66a1f06eaaccb50aeb Mon Sep 17 00:00:00 2001 From: nuclearfog <32868976+nuclearfog@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:57:05 +0200 Subject: [PATCH] fixed fade up/down --- .../org/nuclearfog/apollo/IApolloService.aidl | 2 +- .../apollo/player/MusicPlayerHandler.java | 35 ++++++++++++++----- .../apollo/service/MediaButtonCallback.java | 2 +- .../apollo/service/MusicPlaybackService.java | 21 +++++++---- .../apollo/service/ServiceStub.java | 4 +-- .../nuclearfog/apollo/utils/MusicUtils.java | 2 +- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/app/src/main/aidl/org/nuclearfog/apollo/IApolloService.aidl b/app/src/main/aidl/org/nuclearfog/apollo/IApolloService.aidl index 324d3310..b36907fc 100644 --- a/app/src/main/aidl/org/nuclearfog/apollo/IApolloService.aidl +++ b/app/src/main/aidl/org/nuclearfog/apollo/IApolloService.aidl @@ -8,7 +8,7 @@ interface IApolloService void openFile(in Uri uri); void open(in long [] list, int position); void stop(); - void pause(); + void pause(boolean force); void play(); void prev(); void goToNext(); diff --git a/app/src/main/java/org/nuclearfog/apollo/player/MusicPlayerHandler.java b/app/src/main/java/org/nuclearfog/apollo/player/MusicPlayerHandler.java index 0779cfc8..79207c7d 100644 --- a/app/src/main/java/org/nuclearfog/apollo/player/MusicPlayerHandler.java +++ b/app/src/main/java/org/nuclearfog/apollo/player/MusicPlayerHandler.java @@ -19,10 +19,27 @@ import java.lang.ref.WeakReference; /** + * Player handler for {@link MusicPlaybackService} * + * @author nuclearfog */ public class MusicPlayerHandler extends Handler { + /** + * volume steps used to fade up + */ + private static final float FADE_UP_STEPS = .02f; + + /** + * volume steps used to fade down + */ + private static final float FADE_DOWN_STEPS = .05f; + + /** + * volume step resolution in ms + */ + private static final int FADE_RESOLUTION = 10; + private WeakReference mService; private float mCurrentVolume = 1.0f; @@ -46,24 +63,24 @@ public void handleMessage(@NonNull Message msg) { if (service == null) { return; } - switch (msg.what) { case MESSAGE_FADEDOWN: - mCurrentVolume -= .05f; - if (mCurrentVolume > .2f) { - sendEmptyMessageDelayed(MESSAGE_FADEDOWN, 10); + mCurrentVolume -= FADE_DOWN_STEPS; + if (mCurrentVolume > 0f) { + sendEmptyMessageDelayed(MESSAGE_FADEDOWN, FADE_RESOLUTION); } else { - mCurrentVolume = .2f; + mCurrentVolume = 0f; + service.pause(true); } service.setVolume(mCurrentVolume); break; case MESSAGE_FADEUP: - mCurrentVolume += .01f; - if (mCurrentVolume < 1.0f) { - sendEmptyMessageDelayed(MESSAGE_FADEUP, 10); + mCurrentVolume += FADE_UP_STEPS; + if (mCurrentVolume < 1f) { + sendEmptyMessageDelayed(MESSAGE_FADEUP, FADE_RESOLUTION); } else { - mCurrentVolume = 1.0f; + mCurrentVolume = 1f; } service.setVolume(mCurrentVolume); break; diff --git a/app/src/main/java/org/nuclearfog/apollo/service/MediaButtonCallback.java b/app/src/main/java/org/nuclearfog/apollo/service/MediaButtonCallback.java index 7d1497a0..2f9e8ec4 100644 --- a/app/src/main/java/org/nuclearfog/apollo/service/MediaButtonCallback.java +++ b/app/src/main/java/org/nuclearfog/apollo/service/MediaButtonCallback.java @@ -25,7 +25,7 @@ public void onPlay() { @Override public void onPause() { - service.pause(); + service.pause(false); } @Override diff --git a/app/src/main/java/org/nuclearfog/apollo/service/MusicPlaybackService.java b/app/src/main/java/org/nuclearfog/apollo/service/MusicPlaybackService.java index d1c1ad83..8f485e59 100644 --- a/app/src/main/java/org/nuclearfog/apollo/service/MusicPlaybackService.java +++ b/app/src/main/java/org/nuclearfog/apollo/service/MusicPlaybackService.java @@ -633,7 +633,7 @@ else if (CMDPREVIOUS.equals(command) || ACTION_PREVIOUS.equals(action)) { // pause/play track else if (CMDTOGGLEPAUSE.equals(command) || ACTION_TOGGLEPAUSE.equals(action)) { if (isPlaying()) { - pause(); + pause(false); mPausedByTransientLossOfFocus = false; } else { play(); @@ -641,7 +641,7 @@ else if (CMDTOGGLEPAUSE.equals(command) || ACTION_TOGGLEPAUSE.equals(action)) { } // pause track else if (CMDPAUSE.equals(command) || ACTION_PAUSE.equals(action)) { - pause(); + pause(false); mPausedByTransientLossOfFocus = false; } // play track @@ -650,7 +650,7 @@ else if (CMDPLAY.equals(command)) { } // stop track/dismiss notification else if (CMDSTOP.equals(command) || ACTION_STOP.equals(action)) { - pause(); + pause(true); mPausedByTransientLossOfFocus = false; seek(0); releaseServiceUiAndStop(); @@ -818,8 +818,10 @@ public void play() { gotoNext(true); } mPlayer.start(); + // fade in mPlayerHandler.removeMessages(MESSAGE_FADEDOWN); mPlayerHandler.sendEmptyMessage(MESSAGE_FADEUP); + // if (!mIsSupposedToBePlaying) { mIsSupposedToBePlaying = true; notifyChange(CHANGED_PLAYSTATE); @@ -839,10 +841,15 @@ public void play() { /** * Temporarily pauses playback. */ - public void pause() { - mPlayerHandler.removeMessages(MESSAGE_FADEUP); - if (mIsSupposedToBePlaying) { + public void pause(boolean force) { + if (force) { mPlayer.pause(); + } else { + // use fade out and let MusicPlayerHandler turn off player + mPlayerHandler.removeMessages(MESSAGE_FADEUP); + mPlayerHandler.sendEmptyMessage(MESSAGE_FADEDOWN); + } + if (mIsSupposedToBePlaying) { scheduleDelayedShutdown(); mIsSupposedToBePlaying = false; notifyChange(CHANGED_PLAYSTATE); @@ -986,7 +993,7 @@ public void onTrackEnded() { public void onAudioFocusLoss(int msg) { if (isPlaying()) { mPausedByTransientLossOfFocus = msg == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT; - pause(); + pause(true); } } diff --git a/app/src/main/java/org/nuclearfog/apollo/service/ServiceStub.java b/app/src/main/java/org/nuclearfog/apollo/service/ServiceStub.java index 2002786f..454a72bf 100644 --- a/app/src/main/java/org/nuclearfog/apollo/service/ServiceStub.java +++ b/app/src/main/java/org/nuclearfog/apollo/service/ServiceStub.java @@ -58,10 +58,10 @@ public void stop() { * {@inheritDoc} */ @Override - public void pause() { + public void pause(boolean force) { MusicPlaybackService service = mService.get(); if (service != null) { - service.pause(); + service.pause(force); } } diff --git a/app/src/main/java/org/nuclearfog/apollo/utils/MusicUtils.java b/app/src/main/java/org/nuclearfog/apollo/utils/MusicUtils.java index 7bc1d5dd..75c92730 100644 --- a/app/src/main/java/org/nuclearfog/apollo/utils/MusicUtils.java +++ b/app/src/main/java/org/nuclearfog/apollo/utils/MusicUtils.java @@ -253,7 +253,7 @@ public static void pause() { IApolloService service = mService; if (service != null) { try { - service.pause(); + service.pause(false); } catch (Exception err) { if (BuildConfig.DEBUG) { err.printStackTrace();