Delay loading queue until media is loaded

This commit is contained in:
ByteHamster 2021-08-07 08:28:10 +02:00
parent c3b35f7d8d
commit ad9542f7b6
2 changed files with 37 additions and 22 deletions

View File

@ -77,6 +77,7 @@ import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import de.danoeh.antennapod.core.widget.WidgetUpdater; import de.danoeh.antennapod.core.widget.WidgetUpdater;
import de.danoeh.antennapod.ui.appstartintent.MainActivityStarter; import de.danoeh.antennapod.ui.appstartintent.MainActivityStarter;
import de.danoeh.antennapod.ui.appstartintent.VideoPlayerActivityStarter; import de.danoeh.antennapod.ui.appstartintent.VideoPlayerActivityStarter;
import io.reactivex.Completable;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
@ -306,20 +307,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
npe.printStackTrace(); npe.printStackTrace();
} }
Single.<List<MediaSessionCompat.QueueItem>>create(emitter -> {
List<MediaSessionCompat.QueueItem> queueItems = new ArrayList<>();
for (FeedItem feedItem : taskManager.getQueue()) {
if (feedItem.getMedia() != null) {
MediaDescriptionCompat mediaDescription = feedItem.getMedia().getMediaItem().getDescription();
queueItems.add(new MediaSessionCompat.QueueItem(mediaDescription, feedItem.getId()));
}
}
emitter.onSuccess(queueItems);
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(queueItems -> mediaSession.setQueue(queueItems), Throwable::printStackTrace);
flavorHelper.initializeMediaPlayer(PlaybackService.this); flavorHelper.initializeMediaPlayer(PlaybackService.this);
mediaSession.setActive(true); mediaSession.setActive(true);
@ -367,6 +354,22 @@ public class PlaybackService extends MediaBrowserServiceCompat {
null); // Bundle of optional extras null); // Bundle of optional extras
} }
private void loadQueueForMediaSession() {
Single.<List<MediaSessionCompat.QueueItem>>create(emitter -> {
List<MediaSessionCompat.QueueItem> queueItems = new ArrayList<>();
for (FeedItem feedItem : taskManager.getQueue()) {
if (feedItem.getMedia() != null) {
MediaDescriptionCompat mediaDescription = feedItem.getMedia().getMediaItem().getDescription();
queueItems.add(new MediaSessionCompat.QueueItem(mediaDescription, feedItem.getId()));
}
}
emitter.onSuccess(queueItems);
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(queueItems -> mediaSession.setQueue(queueItems), Throwable::printStackTrace);
}
private MediaBrowserCompat.MediaItem createBrowsableMediaItemForRoot() { private MediaBrowserCompat.MediaItem createBrowsableMediaItemForRoot() {
Uri uri = new Uri.Builder() Uri uri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
@ -414,6 +417,18 @@ public class PlaybackService extends MediaBrowserServiceCompat {
public void onLoadChildren(@NonNull String parentId, public void onLoadChildren(@NonNull String parentId,
@NonNull Result<List<MediaBrowserCompat.MediaItem>> result) { @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
Log.d(TAG, "OnLoadChildren: parentMediaId=" + parentId); Log.d(TAG, "OnLoadChildren: parentMediaId=" + parentId);
result.detach();
Completable.create(emitter -> {
result.sendResult(loadChildrenSynchronous(parentId));
emitter.onComplete();
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> { }, Throwable::printStackTrace);
}
private List<MediaBrowserCompat.MediaItem> loadChildrenSynchronous(@NonNull String parentId) {
List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>(); List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();
if (parentId.equals(getResources().getString(R.string.app_name))) { if (parentId.equals(getResources().getString(R.string.app_name))) {
// Root List // Root List
@ -449,7 +464,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
} }
} }
} }
result.sendResult(mediaItems); return mediaItems;
} }
@Override @Override
@ -815,11 +830,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
mediaPlayer.getPSMPInfo().playerStatus, mediaPlayer.isStreaming()); mediaPlayer.getPSMPInfo().playerStatus, mediaPlayer.isStreaming());
setupNotification(newInfo); setupNotification(newInfo);
break; break;
case PREPARED: case PREPARED:
taskManager.startChapterLoader(newInfo.playable); taskManager.startChapterLoader(newInfo.playable);
break; break;
case PAUSED: case PAUSED:
if ((UserPreferences.isPersistNotify() || isCasting) && if ((UserPreferences.isPersistNotify() || isCasting) &&
android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
@ -833,12 +846,10 @@ public class PlaybackService extends MediaBrowserServiceCompat {
cancelPositionObserver(); cancelPositionObserver();
PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus()); PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus());
break; break;
case STOPPED: case STOPPED:
//writePlaybackPreferencesNoMediaPlaying(); //writePlaybackPreferencesNoMediaPlaying();
//stopService(); //stopService();
break; break;
case PLAYING: case PLAYING:
PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus()); PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus());
setupNotification(newInfo); setupNotification(newInfo);
@ -851,13 +862,14 @@ public class PlaybackService extends MediaBrowserServiceCompat {
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label), EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label),
PlaybackService.this::disableSleepTimer)); PlaybackService.this::disableSleepTimer));
} }
loadQueueForMediaSession();
break; break;
case ERROR: case ERROR:
PlaybackPreferences.writeNoMediaPlaying(); PlaybackPreferences.writeNoMediaPlaying();
stateManager.stopService(); stateManager.stopService();
break; break;
default:
break;
} }
IntentUtils.sendLocalBroadcast(getApplicationContext(), ACTION_PLAYER_STATUS_CHANGED); IntentUtils.sendLocalBroadcast(getApplicationContext(), ACTION_PLAYER_STATUS_CHANGED);

View File

@ -155,7 +155,10 @@ public class PlaybackServiceTaskManager {
/** /**
* Returns the queue or waits until the PSTM has loaded the queue from the database. * Returns the queue or waits until the PSTM has loaded the queue from the database.
*/ */
public synchronized List<FeedItem> getQueue() throws InterruptedException { public List<FeedItem> getQueue() throws InterruptedException {
if (queueFuture == null) {
loadQueue();
}
try { try {
return queueFuture.get(); return queueFuture.get();
} catch (ExecutionException e) { } catch (ExecutionException e) {