rework playback
This commit is contained in:
parent
118a2752cb
commit
bce832c753
6 changed files with 204 additions and 216 deletions
|
|
@ -111,19 +111,19 @@ public class MusicPlayerRemote {
|
||||||
|
|
||||||
public static void playNextSong() {
|
public static void playNextSong() {
|
||||||
if (musicService != null) {
|
if (musicService != null) {
|
||||||
musicService.playNextSong(true);
|
musicService.playNextSong();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void playPreviousSong() {
|
public static void playPreviousSong() {
|
||||||
if (musicService != null) {
|
if (musicService != null) {
|
||||||
musicService.playPreviousSong(true);
|
musicService.playPreviousSong();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void back() {
|
public static void back() {
|
||||||
if (musicService != null) {
|
if (musicService != null) {
|
||||||
musicService.back(true);
|
musicService.back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -143,10 +143,10 @@ public class MusicPlayerRemote {
|
||||||
|
|
||||||
public static void openQueue(final List<Song> queue, final int startPosition, final boolean startPlaying) {
|
public static void openQueue(final List<Song> queue, final int startPosition, final boolean startPlaying) {
|
||||||
if (!tryToHandleOpenPlayingQueue(queue, startPosition) && musicService != null) {
|
if (!tryToHandleOpenPlayingQueue(queue, startPosition) && musicService != null) {
|
||||||
musicService.openQueue(queue, startPosition, startPlaying);
|
|
||||||
if (!PreferenceUtil.getInstance(musicService).getRememberShuffle()) {
|
if (!PreferenceUtil.getInstance(musicService).getRememberShuffle()) {
|
||||||
setShuffleMode(QueueManager.SHUFFLE_MODE_NONE);
|
setShuffleMode(QueueManager.SHUFFLE_MODE_NONE);
|
||||||
}
|
}
|
||||||
|
musicService.openQueue(queue, startPosition, startPlaying);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,8 +157,8 @@ public class MusicPlayerRemote {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tryToHandleOpenPlayingQueue(queue, startPosition) && musicService != null) {
|
if (!tryToHandleOpenPlayingQueue(queue, startPosition) && musicService != null) {
|
||||||
openQueue(queue, startPosition, startPlaying);
|
|
||||||
setShuffleMode(QueueManager.SHUFFLE_MODE_SHUFFLE);
|
setShuffleMode(QueueManager.SHUFFLE_MODE_SHUFFLE);
|
||||||
|
openQueue(queue, startPosition, startPlaying);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,8 +103,6 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
public static final int TRACK_CHANGED = 1;
|
public static final int TRACK_CHANGED = 1;
|
||||||
public static final int TRACK_ENDED = 2;
|
public static final int TRACK_ENDED = 2;
|
||||||
|
|
||||||
public static final int PLAY_SONG = 3;
|
|
||||||
|
|
||||||
public static final int SAVE_QUEUE = 0;
|
public static final int SAVE_QUEUE = 0;
|
||||||
public static final int LOAD_QUEUE = 9;
|
public static final int LOAD_QUEUE = 9;
|
||||||
|
|
||||||
|
|
@ -139,11 +137,14 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
public final QueueManager.QueueCallbacks queueCallbacks = new QueueManager.QueueCallbacks() {
|
public final QueueManager.QueueCallbacks queueCallbacks = new QueueManager.QueueCallbacks() {
|
||||||
@Override
|
@Override
|
||||||
public void onQueueChanged() {
|
public void onQueueChanged() {
|
||||||
|
playback.setQueue(queueManager.getPlayingQueue(), queueManager.getPosition(), queueManager.getRestoredProgress(), queueManager.isResetCurrentSong());
|
||||||
|
saveState();
|
||||||
notifyChange(QUEUE_CHANGED);
|
notifyChange(QUEUE_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRepeatModeChanged() {
|
public void onRepeatModeChanged() {
|
||||||
|
playback.setRepeatMode(queueManager.getRepeatMode());
|
||||||
notifyChange(REPEAT_MODE_CHANGED);
|
notifyChange(REPEAT_MODE_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,8 +176,8 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
acquireWakeLock(30000);
|
acquireWakeLock(30000);
|
||||||
|
|
||||||
if (reason == MEDIA_ITEM_TRANSITION_REASON_AUTO) {
|
if (reason == MEDIA_ITEM_TRANSITION_REASON_AUTO) {
|
||||||
playerHandler.sendEmptyMessage(TRACK_CHANGED);
|
|
||||||
progressHandler.sendEmptyMessage(TRACK_CHANGED);
|
progressHandler.sendEmptyMessage(TRACK_CHANGED);
|
||||||
|
queueManager.setNextPosition();
|
||||||
} else if (reason == MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED) {
|
} else if (reason == MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED) {
|
||||||
progressHandler.sendEmptyMessage(TRACK_CHANGED);
|
progressHandler.sendEmptyMessage(TRACK_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
@ -285,12 +286,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSkipToNext() {
|
public void onSkipToNext() {
|
||||||
playNextSong(true);
|
playNextSong();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSkipToPrevious() {
|
public void onSkipToPrevious() {
|
||||||
back(true);
|
back();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -352,10 +353,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ACTION_REWIND:
|
case ACTION_REWIND:
|
||||||
back(true);
|
back();
|
||||||
break;
|
break;
|
||||||
case ACTION_SKIP:
|
case ACTION_SKIP:
|
||||||
playNextSong(true);
|
playNextSong();
|
||||||
break;
|
break;
|
||||||
case ACTION_STOP:
|
case ACTION_STOP:
|
||||||
case ACTION_QUIT:
|
case ACTION_QUIT:
|
||||||
|
|
@ -417,14 +418,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
queueHandler.removeMessages(SAVE_QUEUE);
|
queueHandler.removeMessages(SAVE_QUEUE);
|
||||||
queueHandler.sendEmptyMessage(SAVE_QUEUE);
|
queueHandler.sendEmptyMessage(SAVE_QUEUE);
|
||||||
|
|
||||||
PreferenceUtil.getInstance(this).setPosition(queueManager.position);
|
|
||||||
PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
|
PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void restoreState() {
|
private void restoreState() {
|
||||||
queueManager.shuffleMode = PreferenceUtil.getInstance(this).getShuffle();
|
|
||||||
queueManager.repeatMode = PreferenceUtil.getInstance(this).getRepeat();
|
|
||||||
|
|
||||||
notifyChange(SHUFFLE_MODE_CHANGED);
|
notifyChange(SHUFFLE_MODE_CHANGED);
|
||||||
notifyChange(REPEAT_MODE_CHANGED);
|
notifyChange(REPEAT_MODE_CHANGED);
|
||||||
|
|
||||||
|
|
@ -432,27 +429,9 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
queueHandler.sendEmptyMessage(LOAD_QUEUE);
|
queueHandler.sendEmptyMessage(LOAD_QUEUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME This will be refactored and partly moved to QueueManager in a subsequent PR
|
|
||||||
private synchronized void restoreQueuesAndPositionIfNecessary() {
|
private synchronized void restoreQueuesAndPositionIfNecessary() {
|
||||||
if (!queuesRestored && queueManager.getPlayingQueue().isEmpty()) {
|
if (!queuesRestored && queueManager.getPlayingQueue().isEmpty()) {
|
||||||
List<Song> restoredQueue = App.getDatabase().queueSongDao().getQueue(0);
|
queueManager.restoreQueue();
|
||||||
List<Song> restoredOriginalQueue = App.getDatabase().queueSongDao().getQueue(1);
|
|
||||||
|
|
||||||
int restoredPosition = PreferenceUtil.getInstance(this).getPosition();
|
|
||||||
int restoredProgress = PreferenceUtil.getInstance(this).getProgress();
|
|
||||||
|
|
||||||
if (restoredQueue.size() > 0 && restoredQueue.size() == restoredOriginalQueue.size() && restoredPosition != -1) {
|
|
||||||
queueManager.originalPlayingQueue = restoredOriginalQueue;
|
|
||||||
queueManager.playingQueue = restoredQueue;
|
|
||||||
|
|
||||||
queueManager.position = restoredPosition;
|
|
||||||
openCurrent();
|
|
||||||
|
|
||||||
if (restoredProgress > 0) seek(restoredProgress);
|
|
||||||
|
|
||||||
handleChangeInternal(META_CHANGED);
|
|
||||||
handleChangeInternal(QUEUE_CHANGED);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
queuesRestored = true;
|
queuesRestored = true;
|
||||||
|
|
@ -487,21 +466,14 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
return playback != null && playback.isLoading();
|
return playback != null && playback.isLoading();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playNextSong(boolean force) {
|
public void playPreviousSong() {
|
||||||
playSongAt(queueManager.getNextPosition(force));
|
queueManager.setPreviousPosition();
|
||||||
|
playback.previous();
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void openTrackAndPrepareNextAt(int position) {
|
public void playNextSong() {
|
||||||
queueManager.position = position;
|
queueManager.setNextPosition();
|
||||||
|
playback.next();
|
||||||
openCurrent();
|
|
||||||
playback.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private synchronized void openCurrent() {
|
|
||||||
if (queueManager.getCurrentSong() == null) return;
|
|
||||||
|
|
||||||
playback.setDataSource(queueManager.getCurrentSong());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initNotification() {
|
public void initNotification() {
|
||||||
|
|
@ -543,7 +515,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
|
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
|
||||||
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
|
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
|
||||||
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
|
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
|
||||||
.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, queueManager.position + 1)
|
.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, queueManager.getPosition() + 1)
|
||||||
.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
|
.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
|
||||||
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
|
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
|
||||||
|
|
||||||
|
|
@ -600,26 +572,24 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
// FIXME This will be refactored and partly moved to QueueManager in a subsequent PR
|
// FIXME This will be refactored and partly moved to QueueManager in a subsequent PR
|
||||||
public void openQueue(@Nullable final List<Song> playingQueue, final int startPosition, final boolean startPlaying) {
|
public void openQueue(@Nullable final List<Song> playingQueue, final int startPosition, final boolean startPlaying) {
|
||||||
if (playingQueue != null && !playingQueue.isEmpty() && startPosition >= 0 && startPosition < playingQueue.size()) {
|
if (playingQueue != null && !playingQueue.isEmpty() && startPosition >= 0 && startPosition < playingQueue.size()) {
|
||||||
// it is important to copy the playing queue here first as we might add or remove songs later
|
|
||||||
queueManager.originalPlayingQueue = new ArrayList<>(playingQueue);
|
queueManager.originalPlayingQueue = new ArrayList<>(playingQueue);
|
||||||
queueManager.playingQueue = new ArrayList<>(queueManager.originalPlayingQueue);
|
queueManager.playingQueue = new ArrayList<>(queueManager.originalPlayingQueue);
|
||||||
|
|
||||||
int position = startPosition;
|
int position = startPosition;
|
||||||
if (queueManager.shuffleMode == QueueManager.SHUFFLE_MODE_SHUFFLE) {
|
if (queueManager.getShuffleMode() == QueueManager.SHUFFLE_MODE_SHUFFLE) {
|
||||||
ShuffleHelper.makeShuffleList(queueManager.playingQueue, startPosition);
|
ShuffleHelper.makeShuffleList(queueManager.playingQueue, startPosition);
|
||||||
position = 0;
|
position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
playSongAt(position);
|
queueManager.setPlayingQueueAndPosition(queueManager.playingQueue, queueManager.originalPlayingQueue, startPosition);
|
||||||
|
if (startPlaying) play();
|
||||||
notifyChange(QUEUE_CHANGED);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playSongAt(final int position) {
|
public void playSongAt(final int position) {
|
||||||
// handle this on the handlers thread to avoid blocking the ui thread
|
queueManager.setPosition(position);
|
||||||
playerHandler.removeMessages(PLAY_SONG);
|
playback.playSongAt(position);
|
||||||
playerHandler.obtainMessage(PLAY_SONG, position, 0).sendToTarget();
|
play();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
|
|
@ -628,25 +598,17 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void play() {
|
public void play() {
|
||||||
if (!playback.isPlaying()) {
|
if (!playback.isPlaying()) {
|
||||||
if (!playback.isReady()) {
|
|
||||||
playSongAt(queueManager.position);
|
|
||||||
} else {
|
|
||||||
playback.start();
|
playback.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void playPreviousSong(boolean force) {
|
public void back() {
|
||||||
playSongAt(queueManager.getPreviousPosition(force));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void back(boolean force) {
|
|
||||||
if (getSongProgressMillis() > 5000) {
|
if (getSongProgressMillis() > 5000) {
|
||||||
seek(0);
|
seek(0);
|
||||||
} else {
|
} else {
|
||||||
playPreviousSong(force);
|
playPreviousSong();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -691,15 +653,13 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
updateNotification();
|
updateNotification();
|
||||||
updateMediaSessionMetadata();
|
updateMediaSessionMetadata();
|
||||||
updateMediaSessionState();
|
updateMediaSessionState();
|
||||||
PreferenceUtil.getInstance(this).setPosition(queueManager.position);
|
PreferenceUtil.getInstance(this).setPosition(queueManager.getPosition());
|
||||||
PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
|
PreferenceUtil.getInstance(this).setProgress(getSongProgressMillis());
|
||||||
break;
|
break;
|
||||||
case QUEUE_CHANGED:
|
case QUEUE_CHANGED:
|
||||||
// because playing queue size might have changed
|
// because playing queue size might have changed
|
||||||
updateMediaSessionMetadata();
|
updateMediaSessionMetadata();
|
||||||
saveState();
|
|
||||||
if (queueManager.getPlayingQueue().size() <= 0) {
|
if (queueManager.getPlayingQueue().size() <= 0) {
|
||||||
playback.pause();
|
|
||||||
playingNotification.stop();
|
playingNotification.stop();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -753,35 +713,20 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case TRACK_CHANGED:
|
|
||||||
if (service.queueManager.getRepeatMode() == QueueManager.REPEAT_MODE_NONE && service.queueManager.isLastTrack()) {
|
|
||||||
service.pause();
|
|
||||||
service.seek(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case TRACK_ENDED:
|
case TRACK_ENDED:
|
||||||
// FIXME This isn't used anywhere. This means releaseWakeLock() is never called
|
// FIXME This isn't used anywhere. This means releaseWakeLock() is never called
|
||||||
|
|
||||||
// if there is a timer finished, don't continue
|
// if there is a timer finished, don't continue
|
||||||
if (service.pendingQuit || service.queueManager.getRepeatMode() == QueueManager.REPEAT_MODE_NONE && service.queueManager.isLastTrack()) {
|
if (service.pendingQuit || service.queueManager.getRepeatMode() == QueueManager.REPEAT_MODE_NONE && service.queueManager.isLastTrack()) {
|
||||||
service.notifyChange(STATE_CHANGED);
|
|
||||||
service.seek(0);
|
|
||||||
if (service.pendingQuit) {
|
if (service.pendingQuit) {
|
||||||
service.pendingQuit = false;
|
service.pendingQuit = false;
|
||||||
service.quit();
|
service.quit();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
service.playNextSong(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
service.releaseWakeLock();
|
service.releaseWakeLock();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PLAY_SONG:
|
|
||||||
service.openTrackAndPrepareNextAt(msg.arg1);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package com.dkanada.gramophone.service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.dkanada.gramophone.App;
|
import com.dkanada.gramophone.App;
|
||||||
import com.dkanada.gramophone.helper.MusicPlayerRemote;
|
|
||||||
import com.dkanada.gramophone.helper.ShuffleHelper;
|
import com.dkanada.gramophone.helper.ShuffleHelper;
|
||||||
import com.dkanada.gramophone.model.Song;
|
import com.dkanada.gramophone.model.Song;
|
||||||
import com.dkanada.gramophone.util.PreferenceUtil;
|
import com.dkanada.gramophone.util.PreferenceUtil;
|
||||||
|
|
@ -13,8 +12,8 @@ import java.util.List;
|
||||||
|
|
||||||
public class QueueManager {
|
public class QueueManager {
|
||||||
public static final int REPEAT_MODE_NONE = 0;
|
public static final int REPEAT_MODE_NONE = 0;
|
||||||
public static final int REPEAT_MODE_ALL = 1;
|
public static final int REPEAT_MODE_THIS = 1;
|
||||||
public static final int REPEAT_MODE_THIS = 2;
|
public static final int REPEAT_MODE_ALL = 2;
|
||||||
|
|
||||||
public static final int SHUFFLE_MODE_NONE = 0;
|
public static final int SHUFFLE_MODE_NONE = 0;
|
||||||
public static final int SHUFFLE_MODE_SHUFFLE = 1;
|
public static final int SHUFFLE_MODE_SHUFFLE = 1;
|
||||||
|
|
@ -25,17 +24,26 @@ public class QueueManager {
|
||||||
List<Song> playingQueue = new ArrayList<>();
|
List<Song> playingQueue = new ArrayList<>();
|
||||||
List<Song> originalPlayingQueue = new ArrayList<>();
|
List<Song> originalPlayingQueue = new ArrayList<>();
|
||||||
|
|
||||||
int position = -1;
|
private int position = 0;
|
||||||
int nextPosition = -1;
|
private int restoredProgress = 0;
|
||||||
|
private boolean resetCurrentSong = true;
|
||||||
|
|
||||||
int shuffleMode;
|
private int shuffleMode;
|
||||||
int repeatMode;
|
private int repeatMode;
|
||||||
|
|
||||||
public QueueManager(Context context, QueueCallbacks callbacks) {
|
public QueueManager(Context context, QueueCallbacks callbacks) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.callbacks = callbacks;
|
this.callbacks = callbacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPlayingQueueAndPosition(List<Song> queue, List<Song> originalPlayingQueue, int position) {
|
||||||
|
this.position = position;
|
||||||
|
this.playingQueue = new ArrayList<>(queue);
|
||||||
|
this.originalPlayingQueue = new ArrayList<>(originalPlayingQueue);
|
||||||
|
|
||||||
|
callbacks.onQueueChanged();
|
||||||
|
}
|
||||||
|
|
||||||
public int getPosition() {
|
public int getPosition() {
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
@ -52,60 +60,32 @@ public class QueueManager {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNextPosition(boolean force) {
|
public void setPosition(int position) {
|
||||||
int position = getPosition() + 1;
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextPosition() {
|
||||||
switch (getRepeatMode()) {
|
switch (getRepeatMode()) {
|
||||||
case REPEAT_MODE_ALL:
|
|
||||||
if (isLastTrack()) {
|
|
||||||
position = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case REPEAT_MODE_THIS:
|
|
||||||
if (force) {
|
|
||||||
if (isLastTrack()) {
|
|
||||||
position = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
position -= 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
case REPEAT_MODE_NONE:
|
case REPEAT_MODE_NONE:
|
||||||
if (isLastTrack()) {
|
|
||||||
position -= 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPreviousPosition(boolean force) {
|
|
||||||
int newPosition = getPosition() - 1;
|
|
||||||
switch (repeatMode) {
|
|
||||||
case REPEAT_MODE_ALL:
|
|
||||||
if (newPosition < 0) {
|
|
||||||
newPosition = getPlayingQueue().size() - 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case REPEAT_MODE_THIS:
|
case REPEAT_MODE_THIS:
|
||||||
if (force) {
|
position = Math.min(position + 1, playingQueue.size() - 1);
|
||||||
if (newPosition < 0) {
|
|
||||||
newPosition = getPlayingQueue().size() - 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
newPosition = getPosition();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
case REPEAT_MODE_ALL:
|
||||||
case REPEAT_MODE_NONE:
|
position = (position + 1) % playingQueue.size();
|
||||||
if (newPosition < 0) {
|
|
||||||
newPosition = 0;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return newPosition;
|
public void setPreviousPosition() {
|
||||||
|
switch (getRepeatMode()) {
|
||||||
|
case REPEAT_MODE_NONE:
|
||||||
|
case REPEAT_MODE_THIS:
|
||||||
|
position = Math.max(position - 1, 0);
|
||||||
|
break;
|
||||||
|
case REPEAT_MODE_ALL:
|
||||||
|
position = (position - 1 + playingQueue.size()) % playingQueue.size();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLastTrack() {
|
public boolean isLastTrack() {
|
||||||
|
|
@ -168,6 +148,7 @@ public class QueueManager {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetCurrentSong = false;
|
||||||
callbacks.onShuffleModeChanged();
|
callbacks.onShuffleModeChanged();
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
@ -175,24 +156,32 @@ public class QueueManager {
|
||||||
public void addSong(int position, Song song) {
|
public void addSong(int position, Song song) {
|
||||||
playingQueue.add(position, song);
|
playingQueue.add(position, song);
|
||||||
originalPlayingQueue.add(position, song);
|
originalPlayingQueue.add(position, song);
|
||||||
|
|
||||||
|
resetCurrentSong = false;
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSong(Song song) {
|
public void addSong(Song song) {
|
||||||
playingQueue.add(song);
|
playingQueue.add(song);
|
||||||
originalPlayingQueue.add(song);
|
originalPlayingQueue.add(song);
|
||||||
|
|
||||||
|
resetCurrentSong = false;
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSongs(int position, List<Song> songs) {
|
public void addSongs(int position, List<Song> songs) {
|
||||||
playingQueue.addAll(position, songs);
|
playingQueue.addAll(position, songs);
|
||||||
originalPlayingQueue.addAll(position, songs);
|
originalPlayingQueue.addAll(position, songs);
|
||||||
|
|
||||||
|
resetCurrentSong = false;
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSongs(List<Song> songs) {
|
public void addSongs(List<Song> songs) {
|
||||||
playingQueue.addAll(songs);
|
playingQueue.addAll(songs);
|
||||||
originalPlayingQueue.addAll(songs);
|
originalPlayingQueue.addAll(songs);
|
||||||
|
|
||||||
|
resetCurrentSong = false;
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,26 +193,21 @@ public class QueueManager {
|
||||||
originalPlayingQueue.remove(playingQueue.remove(position));
|
originalPlayingQueue.remove(playingQueue.remove(position));
|
||||||
}
|
}
|
||||||
|
|
||||||
reposition(position);
|
int currentPosition = getPosition();
|
||||||
callbacks.onQueueChanged();
|
if (position != currentPosition) {
|
||||||
|
resetCurrentSong = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME This will be refactored and removed in a subsequent PR
|
if (position < currentPosition) {
|
||||||
private void reposition(int deletedPosition) {
|
this.position = currentPosition - 1;
|
||||||
int currentPosition = getPosition();
|
|
||||||
if (deletedPosition < currentPosition) {
|
|
||||||
position = currentPosition - 1;
|
|
||||||
} else if (deletedPosition == currentPosition) {
|
|
||||||
if (playingQueue.size() > deletedPosition) {
|
|
||||||
MusicPlayerRemote.playSongAt(position);
|
|
||||||
} else {
|
|
||||||
MusicPlayerRemote.playSongAt(position - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveSong(int from, int to) {
|
public void moveSong(int from, int to) {
|
||||||
if (from == to) return;
|
if (from == to) return;
|
||||||
|
|
||||||
final int currentPosition = getPosition();
|
final int currentPosition = getPosition();
|
||||||
Song songToMove = playingQueue.remove(from);
|
Song songToMove = playingQueue.remove(from);
|
||||||
playingQueue.add(to, songToMove);
|
playingQueue.add(to, songToMove);
|
||||||
|
|
@ -240,6 +224,7 @@ public class QueueManager {
|
||||||
position = to;
|
position = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetCurrentSong = false;
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -247,7 +232,7 @@ public class QueueManager {
|
||||||
playingQueue.clear();
|
playingQueue.clear();
|
||||||
originalPlayingQueue.clear();
|
originalPlayingQueue.clear();
|
||||||
|
|
||||||
position = -1;
|
position = 0;
|
||||||
callbacks.onQueueChanged();
|
callbacks.onQueueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -274,7 +259,24 @@ public class QueueManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void restoreQueue() {
|
||||||
|
position = PreferenceUtil.getInstance(context).getPosition();
|
||||||
|
restoredProgress = PreferenceUtil.getInstance(context).getProgress();
|
||||||
|
|
||||||
|
playingQueue = new ArrayList<>(App.getDatabase().queueSongDao().getQueue(0));
|
||||||
|
originalPlayingQueue = new ArrayList<>(App.getDatabase().queueSongDao().getQueue(1));
|
||||||
|
|
||||||
|
shuffleMode = PreferenceUtil.getInstance(context).getShuffle();
|
||||||
|
repeatMode = PreferenceUtil.getInstance(context).getRepeat();
|
||||||
|
|
||||||
|
callbacks.onQueueChanged();
|
||||||
|
callbacks.onRepeatModeChanged();
|
||||||
|
callbacks.onShuffleModeChanged();
|
||||||
|
}
|
||||||
|
|
||||||
public void saveQueue() {
|
public void saveQueue() {
|
||||||
|
PreferenceUtil.getInstance(context).setPosition(position);
|
||||||
|
|
||||||
// copy queues by value to avoid concurrent modification exceptions from database
|
// copy queues by value to avoid concurrent modification exceptions from database
|
||||||
App.getDatabase().songDao().deleteSongs();
|
App.getDatabase().songDao().deleteSongs();
|
||||||
App.getDatabase().songDao().insertSongs(new ArrayList<>(playingQueue));
|
App.getDatabase().songDao().insertSongs(new ArrayList<>(playingQueue));
|
||||||
|
|
@ -284,6 +286,20 @@ public class QueueManager {
|
||||||
App.getDatabase().queueSongDao().setQueue(new ArrayList<>(originalPlayingQueue), 1);
|
App.getDatabase().queueSongDao().setQueue(new ArrayList<>(originalPlayingQueue), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getRestoredProgress() {
|
||||||
|
int progress = restoredProgress;
|
||||||
|
restoredProgress = 0;
|
||||||
|
|
||||||
|
return progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isResetCurrentSong() {
|
||||||
|
boolean reset = resetCurrentSong;
|
||||||
|
resetCurrentSong = true;
|
||||||
|
|
||||||
|
return reset;
|
||||||
|
}
|
||||||
|
|
||||||
interface QueueCallbacks {
|
interface QueueCallbacks {
|
||||||
void onQueueChanged();
|
void onQueueChanged();
|
||||||
void onRepeatModeChanged();
|
void onRepeatModeChanged();
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@ import com.google.android.exoplayer2.util.MimeTypes;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class LocalPlayer implements Playback {
|
public class LocalPlayer implements Playback {
|
||||||
|
|
@ -41,6 +43,8 @@ public class LocalPlayer implements Playback {
|
||||||
|
|
||||||
private PlaybackCallbacks callbacks;
|
private PlaybackCallbacks callbacks;
|
||||||
|
|
||||||
|
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
@SuppressWarnings("FieldCanBeLocal")
|
@SuppressWarnings("FieldCanBeLocal")
|
||||||
private final EventListener eventListener = new EventListener() {
|
private final EventListener eventListener = new EventListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -64,14 +68,7 @@ public class LocalPlayer implements Playback {
|
||||||
@Override
|
@Override
|
||||||
public void onMediaItemTransition(MediaItem mediaItem, int reason) {
|
public void onMediaItemTransition(MediaItem mediaItem, int reason) {
|
||||||
Log.i(TAG, String.format("onMediaItemTransition: %s %d", mediaItem, reason));
|
Log.i(TAG, String.format("onMediaItemTransition: %s %d", mediaItem, reason));
|
||||||
|
if (callbacks != null) callbacks.onTrackChanged(reason);
|
||||||
if (exoPlayer.getMediaItemCount() > 1) {
|
|
||||||
exoPlayer.removeMediaItem(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (callbacks != null) {
|
|
||||||
callbacks.onTrackChanged(reason);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -115,28 +112,33 @@ public class LocalPlayer implements Playback {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setDataSource(Song song) {
|
public void setQueue(List<Song> queue, int position, int progress, boolean resetCurrentSong) {
|
||||||
MediaItem mediaItem = exoPlayer.getCurrentMediaItem();
|
executorService.submit(() -> {
|
||||||
|
List<MediaItem> mediaItems = createMediaItems(queue);
|
||||||
|
|
||||||
if (mediaItem != null && mediaItem.mediaId.equals(song.id)) {
|
// TODO: Call this on main thread
|
||||||
|
if (resetCurrentSong) {
|
||||||
|
exoPlayer.setMediaItems(mediaItems, position, progress);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
exoPlayer.clearMediaItems();
|
int currentPosition = exoPlayer.getCurrentWindowIndex();
|
||||||
appendDataSource(song);
|
exoPlayer.removeMediaItems(0, currentPosition);
|
||||||
exoPlayer.seekTo(0, 0);
|
|
||||||
|
if (exoPlayer.getMediaItemCount() > 1) {
|
||||||
|
exoPlayer.removeMediaItems(1, exoPlayer.getMediaItemCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
if (position + 1 < mediaItems.size()) {
|
||||||
public void queueDataSource(Song song) {
|
exoPlayer.addMediaItems(1, mediaItems.subList(position + 1, mediaItems.size()));
|
||||||
while (exoPlayer.getMediaItemCount() > 1) {
|
|
||||||
exoPlayer.removeMediaItem(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
appendDataSource(song);
|
exoPlayer.addMediaItems(0, mediaItems.subList(0, position));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendDataSource(Song song) {
|
private List<MediaItem> createMediaItems(List<Song> queue) {
|
||||||
|
return queue.stream().map(song -> {
|
||||||
File audio = new File(MusicUtil.getFileUri(song));
|
File audio = new File(MusicUtil.getFileUri(song));
|
||||||
Uri uri = Uri.fromFile(audio);
|
Uri uri = Uri.fromFile(audio);
|
||||||
|
|
||||||
|
|
@ -147,21 +149,14 @@ public class LocalPlayer implements Playback {
|
||||||
List<String> containers = PreferenceUtil.getInstance(context).getDirectPlayCodecs().stream()
|
List<String> containers = PreferenceUtil.getInstance(context).getDirectPlayCodecs().stream()
|
||||||
.map(codec -> codec.container.toLowerCase(Locale.ROOT))
|
.map(codec -> codec.container.toLowerCase(Locale.ROOT))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
List<String> codecs = PreferenceUtil.getInstance(context).getDirectPlayCodecs().stream()
|
List<String> codecs = PreferenceUtil.getInstance(context).getDirectPlayCodecs().stream()
|
||||||
.map(codec -> codec.codec.toLowerCase(Locale.ROOT))
|
.map(codec -> codec.codec.toLowerCase(Locale.ROOT))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
String maxBitrate = PreferenceUtil.getInstance(context).getMaximumBitrate();
|
String maxBitrate = PreferenceUtil.getInstance(context).getMaximumBitrate();
|
||||||
|
|
||||||
MediaItem mediaItem;
|
MediaItem mediaItem;
|
||||||
|
|
||||||
boolean shouldDirectPlay =
|
if (uri.toString().contains("file://") || (containers.contains(song.container.toLowerCase(Locale.ROOT)) && codecs.contains(song.codec.toLowerCase(Locale.ROOT)) && song.bitRate <= Integer.parseInt(maxBitrate))) {
|
||||||
containers.contains(song.container.toLowerCase(Locale.ROOT)) &&
|
|
||||||
codecs.contains(song.codec.toLowerCase(Locale.ROOT)) &&
|
|
||||||
song.bitRate <= Integer.parseInt(maxBitrate);
|
|
||||||
|
|
||||||
if (uri.toString().contains("file://") || shouldDirectPlay || !song.supportsTranscoding) {
|
|
||||||
mediaItem = new MediaItem.Builder()
|
mediaItem = new MediaItem.Builder()
|
||||||
.setUri(uri)
|
.setUri(uri)
|
||||||
.setMediaId(song.id)
|
.setMediaId(song.id)
|
||||||
|
|
@ -174,7 +169,15 @@ public class LocalPlayer implements Playback {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
exoPlayer.addMediaItem(mediaItem);
|
return mediaItem;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playSongAt(int position) {
|
||||||
|
if (exoPlayer.getMediaItemCount() > 0) {
|
||||||
|
exoPlayer.seekTo(Math.max(0, position) % exoPlayer.getMediaItemCount(), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource.Factory buildDataSourceFactory() {
|
private DataSource.Factory buildDataSourceFactory() {
|
||||||
|
|
@ -230,6 +233,21 @@ public class LocalPlayer implements Playback {
|
||||||
exoPlayer.release();
|
exoPlayer.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void previous() {
|
||||||
|
exoPlayer.previous();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void next() {
|
||||||
|
exoPlayer.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRepeatMode(@Player.RepeatMode int repeatMode) {
|
||||||
|
exoPlayer.setRepeatMode(repeatMode);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getProgress() {
|
public int getProgress() {
|
||||||
return (int) exoPlayer.getCurrentPosition();
|
return (int) exoPlayer.getCurrentPosition();
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
package com.dkanada.gramophone.service.playback;
|
package com.dkanada.gramophone.service.playback;
|
||||||
|
|
||||||
import com.dkanada.gramophone.model.Song;
|
import com.dkanada.gramophone.model.Song;
|
||||||
|
import com.google.android.exoplayer2.Player;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface Playback {
|
public interface Playback {
|
||||||
void setDataSource(Song song);
|
void setQueue(List<Song> queue, int position, int progress, boolean resetCurrentSong);
|
||||||
|
|
||||||
void queueDataSource(Song song);
|
void playSongAt(int position);
|
||||||
|
|
||||||
void setCallbacks(PlaybackCallbacks callbacks);
|
void setCallbacks(PlaybackCallbacks callbacks);
|
||||||
|
|
||||||
|
|
@ -21,6 +24,12 @@ public interface Playback {
|
||||||
|
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
|
void previous();
|
||||||
|
|
||||||
|
void next();
|
||||||
|
|
||||||
|
void setRepeatMode(@Player.RepeatMode int repeatMode);
|
||||||
|
|
||||||
int getProgress();
|
int getProgress();
|
||||||
|
|
||||||
int getDuration();
|
int getDuration();
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ public final class PreferenceUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPosition() {
|
public int getPosition() {
|
||||||
return mPreferences.getInt(POSITION, -1);
|
return mPreferences.getInt(POSITION, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPosition(int position) {
|
public void setPosition(int position) {
|
||||||
|
|
@ -178,7 +178,7 @@ public final class PreferenceUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getProgress() {
|
public int getProgress() {
|
||||||
return mPreferences.getInt(PROGRESS, -1);
|
return mPreferences.getInt(PROGRESS, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProgress(int progress) {
|
public void setProgress(int progress) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue