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

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Apr 28, 2024
1 parent 609ace5 commit bfa27d6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 77 deletions.
85 changes: 46 additions & 39 deletions app/src/main/java/org/nuclearfog/apollo/player/MultiPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.nuclearfog.apollo.service.MusicPlaybackService;
import org.nuclearfog.apollo.BuildConfig;
import org.nuclearfog.apollo.utils.PreferenceUtils;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -50,7 +50,6 @@ public class MultiPlayer implements OnErrorListener, OnCompletionListener {
* indicates that two track are crossfading
*/
private static final int XFADE = 12;

/**
* sampling rate of the fade effect
*/
Expand Down Expand Up @@ -89,7 +88,7 @@ public class MultiPlayer implements OnErrorListener, OnCompletionListener {
/**
* volume of the current mediaplayer
*/
private float currentVolume = 0.0f;
private float currentVolume = 0f;
/**
* current mediaplayer's index
*/
Expand Down Expand Up @@ -125,11 +124,12 @@ public MultiPlayer(Service service) {
*/
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "onError:" + what + ", " + extra);
if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
mp.reset();
initialized = false;
playerHandler.sendMessageDelayed(playerHandler.obtainMessage(MusicPlaybackService.MESSAGE_SERVER_DIED), 2000);
Log.d(TAG, "onError:" + what + "," + extra);
xfadeMode = NONE;
playerHandler.sendEmptyMessageDelayed(MusicPlayerHandler.MESSAGE_SERVER_DIED, 2000);
return true;
}
return false;
Expand All @@ -144,10 +144,11 @@ public void onCompletion(MediaPlayer mp) {
// switch to next player
mPlayers[currentPlayer].reset();
currentPlayer = (currentPlayer + 1) % mPlayers.length;
//
playerHandler.sendEmptyMessage(MusicPlaybackService.MESSAGE_TRACK_WENT_TO_NEXT);
// notify playback service that track went to next
playerHandler.sendEmptyMessage(MusicPlayerHandler.MESSAGE_TRACK_WENT_TO_NEXT);
} else {
playerHandler.sendEmptyMessage(MusicPlaybackService.MESSAGE_TRACK_ENDED);
// notify playback service that the track ended
playerHandler.sendEmptyMessage(MusicPlayerHandler.MESSAGE_TRACK_ENDED);
}
}

Expand All @@ -166,10 +167,10 @@ public void setDataSource(@NonNull Uri uri) {
public void setNextDataSource(@NonNull Uri uri) {
int nextPlayerIndex;
// if there is a crossfade pending, use the mediaplayer after next
if (xfadeMode == XFADE)
nextPlayerIndex = (currentPlayer + 2) % mPlayers.length;
else
if (xfadeMode == NONE)
nextPlayerIndex = (currentPlayer + 1) % mPlayers.length;
else
nextPlayerIndex = (currentPlayer + 2) % mPlayers.length;
setDataSourceImpl(mPlayers[nextPlayerIndex], uri);
}

Expand All @@ -195,11 +196,11 @@ public boolean isInitialized() {
public void play() {
MediaPlayer mp = mPlayers[currentPlayer];
if (!mp.isPlaying()) {
currentVolume = 0.0f;
setCrossfadeTask(true);
mp.setVolume(currentVolume, currentVolume);
mp.start();
xfadeMode = FADE_IN;
currentVolume = 0f;
mp.setVolume(0f, 0f);
mp.start();
setCrossfadeTask(true);
}
}

Expand All @@ -211,9 +212,11 @@ public void play() {
public void pause(boolean force) {
MediaPlayer mp = mPlayers[currentPlayer];
if (force) {
if (mp.isPlaying())
mp.pause();
xfadeMode = NONE;
setCrossfadeTask(false);
if (mp.isPlaying()) {
mp.pause();
}
} else {
xfadeMode = FADE_OUT;
}
Expand All @@ -236,6 +239,7 @@ public void next() {
if (!mp.isPlaying()) {
mp.setVolume(0f, 0f);
mp.start();
setCrossfadeTask(true);
}
xfadeMode = XFADE;
}
Expand Down Expand Up @@ -270,12 +274,12 @@ public long getPosition() {
}

/**
* Gets the current playback position.
* Sets the current playback position.
*
* @param whereto The offset in milliseconds from the start to seek to
* @param position The offset in milliseconds from the start to seek to
*/
public void seek(long whereto) {
mPlayers[currentPlayer].seekTo((int) whereto);
public void setPosition(long position) {
mPlayers[currentPlayer].seekTo((int) position);
}

/**
Expand Down Expand Up @@ -326,7 +330,7 @@ private boolean setDataSourceImpl(MediaPlayer player, @NonNull Uri uri) {
if (mPreferences.isExternalAudioFxPrefered() && !mPreferences.isAudioFxEnabled()) {
Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.getAudioSessionId());
intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, MusicPlaybackService.APOLLO_PACKAGE_NAME);
intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, BuildConfig.APPLICATION_ID);
context.sendBroadcast(intent);
}
return true;
Expand All @@ -341,30 +345,19 @@ private void onCrossfadeTrack() {
long diff = getDuration() - getPosition();
MediaPlayer current = mPlayers[currentPlayer];
MediaPlayer next = mPlayers[(currentPlayer + 1) % mPlayers.length];
// crossfade current and next playback
if ((getPosition() > XFADE_DELAY && diff < XFADE_DELAY)) {
// calc volume for current and next player
float volume = Math.max((float) diff / XFADE_DELAY, 0f);
float invert = 1f - volume;
// fade down current player
current.setVolume(volume, volume);
// fade up next player
next.setVolume(invert, invert);
// start next player
if (!next.isPlaying()) {
next.start();
}
}
// force crossfade to next track
else if (xfadeMode == XFADE) {
if (xfadeMode == XFADE) {
currentVolume = Math.max(currentVolume - FADE_STEPS, 0f);
float invert = 1f - currentVolume;
current.setVolume(currentVolume, currentVolume);
next.setVolume(invert, invert);
if (currentVolume < FADE_THRESHOLD) {
current.setVolume(0f, 0f);
next.setVolume(1f, 1f);
onCompletion(current);
currentVolume = 1f;
xfadeMode = NONE;
} else {
current.setVolume(currentVolume, currentVolume);
next.setVolume(invert, invert);
}
}
// fade out current playback
Expand All @@ -384,6 +377,20 @@ else if (xfadeMode == FADE_IN) {
xfadeMode = NONE;
}
}
// crossfade current and next playback
else if ((getPosition() > XFADE_DELAY && diff < XFADE_DELAY)) {
// calc volume for current and next player
float volume = Math.max((float) diff / XFADE_DELAY, 0f);
float invert = 1f - volume;
// fade down current player
current.setVolume(volume, volume);
// fade up next player
next.setVolume(invert, invert);
// start next player
if (!next.isPlaying()) {
next.start();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.nuclearfog.apollo.player;

import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static org.nuclearfog.apollo.service.MusicPlaybackService.MESSAGE_FOCUS_CHANGE;
import static org.nuclearfog.apollo.service.MusicPlaybackService.MESSAGE_SERVER_DIED;
import static org.nuclearfog.apollo.service.MusicPlaybackService.MESSAGE_TRACK_ENDED;
import static org.nuclearfog.apollo.service.MusicPlaybackService.MESSAGE_TRACK_WENT_TO_NEXT;

import android.media.AudioManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Process;

import androidx.annotation.NonNull;

Expand All @@ -24,12 +19,26 @@
* @author nuclearfog
*/
public class MusicPlayerHandler extends Handler {

/**
*
*/
private static final String HANDLER_NAME = "MusicPlayerHandler";

/**
* Indicates the player died
*/
public static final int MESSAGE_SERVER_DIED = 0xA2F4FFEE;
/**
* Indicates that the current track was changed the next track
*/
public static final int MESSAGE_TRACK_WENT_TO_NEXT = 0xB4C13964;
/**
* Indicates when the track ends
*/
public static final int MESSAGE_TRACK_ENDED = 0xF7E68B1A;
/**
* Indicates some sort of focus change, maybe a phone call
*/
public static final int MESSAGE_FOCUS_CHANGE = 0xDB9F6A3B;

private WeakReference<MusicPlaybackService> mService;

Expand Down Expand Up @@ -88,7 +97,7 @@ public void handleMessage(@NonNull Message msg) {
* initialize MusicPlayerHandler class
*/
public static MusicPlayerHandler init(MusicPlaybackService service) {
HandlerThread thread = new HandlerThread(HANDLER_NAME, THREAD_PRIORITY_BACKGROUND);
HandlerThread thread = new HandlerThread(HANDLER_NAME, Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
return new MusicPlayerHandler(service, thread.getLooper());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,6 @@ public class MusicPlaybackService extends MediaBrowserServiceCompat implements O
* Repeats all the tracks in a list
*/
public static final int REPEAT_ALL = 0xEE3F9E0B;
/**
* Indicates when the track ends
*/
public static final int MESSAGE_TRACK_ENDED = 0xF7E68B1A;
/**
* Indicates that the current track was changed the next track
*/
public static final int MESSAGE_TRACK_WENT_TO_NEXT = 0xB4C13964;
/**
* Indicates the player died
*/
public static final int MESSAGE_SERVER_DIED = 0xA2F4FFEE;
/**
* Indicates some sort of focus change, maybe a phone call
*/
public static final int MESSAGE_FOCUS_CHANGE = 0xDB9F6A3B;
/**
* Idle time before stopping the foreground notfication (1 minute)
*/
Expand Down Expand Up @@ -418,7 +402,7 @@ public boolean onUnbind(Intent intent) {
// before stopping the service, so that pause/resume isn't slow.
// Also delay stopping the service if we're transitioning between
// tracks.
} else if (!mPlayList.isEmpty() || mPlayerHandler.hasMessages(MESSAGE_TRACK_ENDED)) {
} else if (!mPlayList.isEmpty() || mPlayerHandler.hasMessages(MusicPlayerHandler.MESSAGE_TRACK_ENDED)) {
scheduleDelayedShutdown();
return true;
}
Expand Down Expand Up @@ -562,7 +546,7 @@ public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaB
*/
@Override
public void onAudioFocusChange(int focusChange) {
mPlayerHandler.obtainMessage(MESSAGE_FOCUS_CHANGE, focusChange, 0).sendToTarget();
mPlayerHandler.obtainMessage(MusicPlayerHandler.MESSAGE_FOCUS_CHANGE, focusChange, 0).sendToTarget();
}

/**
Expand Down Expand Up @@ -846,7 +830,6 @@ public void gotoNext(boolean force) {
}
} else {
mPlayer.next();
setNextTrack();
notifyChange(CHANGED_META);
}
}
Expand Down Expand Up @@ -877,7 +860,7 @@ public long seek(long position) {
} else if (position > mPlayer.getDuration()) {
position = mPlayer.getDuration();
}
mPlayer.seek(position);
mPlayer.setPosition(position);
notifyChange(CHANGED_POSITION);
setPlaybackState(isPlaying());
return position;
Expand Down Expand Up @@ -1098,7 +1081,6 @@ else if (mShuffleMode == SHUFFLE_NORMAL) {
* @param index The position to place the track
*/
synchronized void setQueuePosition(int index) {
stop(false);
mPlayPos = index;
openCurrentAndNext();
play();
Expand Down Expand Up @@ -1287,7 +1269,7 @@ synchronized void prev() {
}
}
stop(false);
openCurrentTrack();
openCurrentAndNext();
play();
notifyChange(CHANGED_META);
}
Expand Down Expand Up @@ -1812,13 +1794,12 @@ private boolean wasRecentlyUsed(int idx, int lookbacksize) {
*
*/
private void releaseServiceUiAndStop() {
if (isPlaying() || mPausedByTransientLossOfFocus || mPlayerHandler.hasMessages(MESSAGE_TRACK_ENDED)) {
return;
}
stopForeground(true);
if (!mServiceInUse) {
saveQueue(true);
stopSelf(mServiceStartId);
if (!isPlaying() && !mPausedByTransientLossOfFocus && !mPlayerHandler.hasMessages(MusicPlayerHandler.MESSAGE_TRACK_ENDED)) {
stopForeground(true);
if (!mServiceInUse) {
saveQueue(true);
stopSelf(mServiceStartId);
}
}
}

Expand Down

0 comments on commit bfa27d6

Please sign in to comment.