|
| 1 | +/* |
| 2 | + This file is part of Subsonic. |
| 3 | + Subsonic is free software: you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation, either version 3 of the License, or |
| 6 | + (at your option) any later version. |
| 7 | + Subsonic is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | + You should have received a copy of the GNU General Public License |
| 12 | + along with Subsonic. If not, see <http://www.gnu.org/licenses/>. |
| 13 | + Copyright 2014 (C) Scott Jackson |
| 14 | +*/ |
| 15 | + |
| 16 | +package github.paroj.dsub2000.util.compat; |
| 17 | + |
| 18 | +import android.os.Handler; |
| 19 | +import android.os.Looper; |
| 20 | +import androidx.annotation.NonNull; |
| 21 | +import androidx.annotation.OptIn; |
| 22 | +import androidx.media3.common.Player; |
| 23 | +import androidx.media3.common.SimpleBasePlayer; |
| 24 | +import androidx.media3.common.util.UnstableApi; |
| 25 | +import github.paroj.dsub2000.service.DownloadService; |
| 26 | +import github.paroj.dsub2000.service.DownloadFile; |
| 27 | +import com.google.common.util.concurrent.Futures; |
| 28 | +import com.google.common.util.concurrent.ListenableFuture; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +@OptIn(markerClass = UnstableApi.class) |
| 33 | +public class DSubPlayer extends SimpleBasePlayer { |
| 34 | + private final DownloadService downloadService; |
| 35 | + private State state; |
| 36 | + |
| 37 | + public DSubPlayer(Looper looper, DownloadService downloadService) { |
| 38 | + super(looper); |
| 39 | + this.downloadService = downloadService; |
| 40 | + state = new State.Builder() |
| 41 | + .setAvailableCommands(new Player.Commands.Builder().addAllCommands().build()) |
| 42 | + .setPlaylist(Collections.emptyList()) |
| 43 | + .build(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected State getState() { |
| 48 | + return state; |
| 49 | + } |
| 50 | + |
| 51 | + public void setInternalState(State newState) { |
| 52 | + if (Looper.myLooper() == getApplicationLooper()) { |
| 53 | + state = newState; |
| 54 | + invalidateState(); |
| 55 | + } else { |
| 56 | + new Handler(getApplicationLooper()).post(() -> setInternalState(newState)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public interface UpdateAction { |
| 61 | + void update(State.Builder builder); |
| 62 | + } |
| 63 | + |
| 64 | + public void updateState(UpdateAction action) { |
| 65 | + if (Looper.myLooper() == getApplicationLooper()) { |
| 66 | + State.Builder builder = state.buildUpon(); |
| 67 | + action.update(builder); |
| 68 | + setInternalState(builder.build()); |
| 69 | + } else { |
| 70 | + new Handler(getApplicationLooper()).post(() -> updateState(action)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public State.Builder getStateBuilder() { |
| 75 | + return state.buildUpon(); |
| 76 | + } |
| 77 | + |
| 78 | + @NonNull |
| 79 | + @Override |
| 80 | + protected ListenableFuture<?> handleSetPlayWhenReady(boolean playWhenReady) { |
| 81 | + if (playWhenReady) { |
| 82 | + downloadService.start(); |
| 83 | + } else { |
| 84 | + downloadService.pause(); |
| 85 | + } |
| 86 | + return Futures.immediateVoidFuture(); |
| 87 | + } |
| 88 | + |
| 89 | + @NonNull |
| 90 | + @Override |
| 91 | + protected ListenableFuture<?> handleSeek(int mediaItemIndex, long positionMs, int seekCommand) { |
| 92 | + if (mediaItemIndex != state.currentMediaItemIndex) { |
| 93 | + List<DownloadFile> queue = downloadService.getSongs(); |
| 94 | + if (queue != null && mediaItemIndex >= 0 && mediaItemIndex < queue.size()) { |
| 95 | + downloadService.play(queue.get(mediaItemIndex)); |
| 96 | + } |
| 97 | + } else { |
| 98 | + downloadService.seekTo((int) positionMs); |
| 99 | + } |
| 100 | + return Futures.immediateVoidFuture(); |
| 101 | + } |
| 102 | +} |
0 commit comments