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