Issue/play from here (#713)

* Fixes race condition in MediaFileListFragment

The "Play from here" feature previously queued all songs at once using
a loop and did not wait until an add action was confirmed by Kodi
before queueing the next item. This sometimes resulted in a seemingly
random play order on Kodi.

* Fixed play from here adding first item twice

When selecting "Play from here" in the file browser the current
item is started using "playMediaFile" and the subsequent items
 are queued. However, the current item was added to the queue
list as well. Resulting in the first item always being added twice
to the playlist.
This commit is contained in:
Martijn Brekhof 2020-02-20 20:53:00 +01:00 committed by GitHub
parent e92722375c
commit e63fd40d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 25 deletions

View File

@ -82,7 +82,7 @@ public class MediaFileListFragment extends AbstractListFragment {
ListType.Sort sortMethod = null;
String parentDirectory = null;
int playlistId = PlaylistType.MUSIC_PLAYLISTID; // this is the ID of the music player
// private MediaFileListAdapter adapter = null;
// private MediaFileListAdapter adapter = null;
boolean browseRootAlready = false;
FileLocation loadOnVisible = null;
@ -366,9 +366,9 @@ public class MediaFileListFragment extends AbstractListFragment {
action.execute(hostManager.getConnection(), new ApiCallback<String>() {
@Override
public void onSuccess(String result) {
while (!mediaQueueFileLocation.isEmpty()) {
queueMediaFile(mediaQueueFileLocation.poll().file);
}
HostConnection connection = hostManager.getConnection();
startPlaylistIfNoActivePlayers(connection, playlistId, callbackHandler);
callbackHandler.post(queueMediaQueueFileLocations);
}
@Override
@ -381,6 +381,33 @@ public class MediaFileListFragment extends AbstractListFragment {
}, callbackHandler);
}
private Runnable queueMediaQueueFileLocations = new Runnable() {
@Override
public void run() {
if (!mediaQueueFileLocation.isEmpty()) {
final HostConnection connection = hostManager.getConnection();
PlaylistType.Item item = new PlaylistType.Item();
item.file = mediaQueueFileLocation.poll().file;
Playlist.Add action = new Playlist.Add(playlistId, item);
action.execute(connection, new ApiCallback<String>() {
@Override
public void onSuccess(String result ) {
callbackHandler.post(queueMediaQueueFileLocations);
}
@Override
public void onError(int errorCode, String description) {
if (!isAdded()) return;
Toast.makeText(getActivity(),
String.format(getString(R.string.error_queue_media_file), description),
Toast.LENGTH_SHORT).show();
callbackHandler.post(queueMediaQueueFileLocations);
}
}, callbackHandler);
}
}
};
/**
* Starts playing the given media file on the local device
* @param filename Filename to start playing
@ -554,7 +581,6 @@ public class MediaFileListFragment extends AbstractListFragment {
mediaQueueFileLocation.clear();
FileLocation fl;
// start playing the selected one, then queue the rest
mediaQueueFileLocation.add(loc);
for (int i = position + 1; i < fileLocationItems.size(); i++) {
fl = fileLocationItems.get(i);
if (!fl.isDirectory) {