fix: put queue into background thread

This commit is contained in:
eddyizm 2025-11-23 09:09:20 -08:00
parent 3496918ce6
commit 8188ef169c
No known key found for this signature in database
GPG key ID: CF5F671829E8158A

View file

@ -2,6 +2,8 @@ package com.cappielloantonio.tempo.service;
import android.content.ComponentName;
import android.util.Log;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -36,6 +38,8 @@ import com.google.common.util.concurrent.MoreExecutors;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
public class MediaManager {
@ -43,6 +47,8 @@ public class MediaManager {
private static WeakReference<MediaBrowser> attachedBrowserRef = new WeakReference<>(null);
public static AtomicBoolean justStarted = new AtomicBoolean(false);
private static final ExecutorService backgroundExecutor = Executors.newSingleThreadExecutor();
public static void registerPlaybackObserver(
ListenableFuture<MediaBrowser> browserFuture,
PlaybackViewModel playbackViewModel
@ -175,16 +181,21 @@ public class MediaManager {
}
}
@OptIn(markerClass = UnstableApi.class)
public static void startQueue(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture, List<Child> media, int startIndex) {
if (mediaBrowserListenableFuture != null) {
mediaBrowserListenableFuture.addListener(() -> {
try {
if (mediaBrowserListenableFuture.isDone()) {
MediaBrowser browser = mediaBrowserListenableFuture.get();
justStarted.set(true);
browser.setMediaItems(MappingUtil.mapMediaItems(media), startIndex, 0);
browser.prepare();
final MediaBrowser browser = mediaBrowserListenableFuture.get();
backgroundExecutor.execute(() -> {
final List<MediaItem> items = MappingUtil.mapMediaItems(media);
enqueueDatabase(media, true, 0);
new Handler(Looper.getMainLooper()).post(() -> {
justStarted.set(true);
browser.setMediaItems(items, startIndex, 0);
browser.prepare();
Player.Listener timelineListener = new Player.Listener() {
@Override
public void onTimelineChanged(Timeline timeline, int reason) {
@ -197,14 +208,16 @@ public class MediaManager {
}
};
browser.addListener(timelineListener);
enqueueDatabase(media, true, 0);
});
});
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
Log.e(TAG, "Error executing startQueue logic: " + e.getMessage(), e);
}
}, MoreExecutors.directExecutor());
}
}
public static void startQueue(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture, Child media) {