Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
fixed fade up/down
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Apr 22, 2024
1 parent 2d7f767 commit fbd305c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MusicPlaybackService> mService;
private float mCurrentVolume = 1.0f;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onPlay() {

@Override
public void onPause() {
service.pause();
service.pause(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,15 @@ 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();
}
}
// pause track
else if (CMDPAUSE.equals(command) || ACTION_PAUSE.equals(action)) {
pause();
pause(false);
mPausedByTransientLossOfFocus = false;
}
// play track
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -986,7 +993,7 @@ public void onTrackEnded() {
public void onAudioFocusLoss(int msg) {
if (isPlaying()) {
mPausedByTransientLossOfFocus = msg == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT;
pause();
pause(true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fbd305c

Please sign in to comment.