Skip to content

Commit 405506a

Browse files
committed
Fixed #75
1 parent 99ef434 commit 405506a

4 files changed

Lines changed: 95 additions & 23 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ private void handleFrame(@NotNull Spirc.MessageType type, @NotNull Spirc.Frame s
133133
handleSeek(frame.value.getAsInt());
134134
break;
135135
case kMessageTypeReplace:
136-
if (frame != null && frame.endpoint == Remote3Frame.Endpoint.UpdateContext) {
137-
updatedTracks(frame);
136+
if (frame == null) break;
137+
138+
if (frame.endpoint == Remote3Frame.Endpoint.UpdateContext) {
139+
updateContext(frame);
138140
stateUpdated();
141+
} else if (frame.endpoint == Remote3Frame.Endpoint.SetQueue) {
142+
updateTracks(frame);
139143
}
140144
break;
141145
case kMessageTypeRepeat:
@@ -268,9 +272,14 @@ private void handleSeek(int pos) {
268272
stateUpdated();
269273
}
270274

271-
private void updatedTracks(@NotNull Remote3Frame frame) {
275+
private void updateTracks(@NotNull Remote3Frame frame) {
276+
state.updateTracks(frame);
277+
stateUpdated();
278+
}
279+
280+
private void updateContext(@NotNull Remote3Frame frame) {
272281
if (frame.context.uri != null) {
273-
state.update(frame);
282+
state.updateContext(frame);
274283

275284
String context = frame.context.uri;
276285
if (context.startsWith("spotify:station:") || context.startsWith("spotify:dailymix:"))
@@ -369,7 +378,7 @@ private void handleLoad(@NotNull Remote3Frame frame) {
369378

370379
LOGGER.debug(String.format("Loading context, uri: %s", frame.context.uri));
371380

372-
updatedTracks(frame);
381+
updateContext(frame);
373382

374383
if (state.getTrackCount() > 0) {
375384
state.setPositionMs(frame.options.seekTo == -1 ? 0 : frame.options.seekTo);
@@ -466,7 +475,7 @@ private void loadAutoplay() {
466475

467476
try {
468477
MercuryRequests.StationsWrapper json = session.mercury().sendSync(MercuryRequests.getStationFor(context));
469-
state.update(json);
478+
state.updateContext(json);
470479

471480
state.setPositionMs(0);
472481
state.setPositionMeasuredAt(TimeProvider.currentTimeMillis());
@@ -608,7 +617,31 @@ void seekTo(@Nullable String uri) {
608617
state.setPlayingTrackIndex(pos);
609618
}
610619

611-
void update(@NotNull Remote3Frame frame) {
620+
void updateTracks(@NotNull Remote3Frame frame) {
621+
if (state.getTrackCount() == 0) {
622+
LOGGER.warn("Couldn't update tracks, there are none right now!");
623+
return;
624+
}
625+
626+
Spirc.TrackRef current = state.getTrack(state.getPlayingTrackIndex());
627+
state.clearTrack();
628+
629+
if (frame.prevTracks != null) {
630+
for (Remote3Track track : frame.prevTracks)
631+
track.addToState(state);
632+
}
633+
634+
if (current != null)
635+
state.addTrack(current);
636+
state.setPlayingTrackIndex(state.getTrackCount() - 1);
637+
638+
if (frame.nextTracks != null) {
639+
for (Remote3Track track : frame.nextTracks)
640+
track.addToState(state);
641+
}
642+
}
643+
644+
void updateContext(@NotNull Remote3Frame frame) {
612645
if (frame.context == null)
613646
throw new IllegalArgumentException("Invalid frame received!");
614647

@@ -644,7 +677,7 @@ void update(@NotNull Remote3Frame frame) {
644677
List<Remote3Track> tracks = frame.context.pages.get(pageIndex).tracks;
645678
for (int i = 0; i < tracks.size(); i++) {
646679
Remote3Track track = tracks.get(i);
647-
state.addTrack(track.toTrackRef());
680+
track.addToState(state);
648681

649682
if (Objects.equals(trackUid, track.uri) || Objects.equals(trackUid, track.uid))
650683
index = i;
@@ -664,7 +697,7 @@ void update(@NotNull Remote3Frame frame) {
664697
return;
665698
}
666699

667-
for (Remote3Track track : tracks) state.addTrack(track.toTrackRef());
700+
for (Remote3Track track : tracks) track.addToState(state);
668701

669702
if (frame.options != null && frame.options.skipTo != null && frame.options.skipTo.trackIndex != -1)
670703
state.setPlayingTrackIndex(frame.options.skipTo.trackIndex);
@@ -678,7 +711,7 @@ void update(@NotNull Remote3Frame frame) {
678711
}
679712
}
680713

681-
void update(@NotNull MercuryRequests.StationsWrapper json) {
714+
void updateContext(@NotNull MercuryRequests.StationsWrapper json) {
682715
state.setContextUri(json.uri());
683716

684717
state.setPlayingTrackIndex(0);
@@ -717,5 +750,7 @@ void setPlayingTrackIndex(int i) {
717750
int getTrackCount() {
718751
return state.getTrackCount();
719752
}
753+
754+
720755
}
721756
}

core/src/main/java/xyz/gianlu/librespot/player/remote/Remote3Frame.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class Remote3Frame {
3434
public final Options options;
3535
public final JsonPrimitive value;
3636
public final Remote3Track track;
37+
public final List<Remote3Track> prevTracks;
38+
public final List<Remote3Track> nextTracks;
3739

3840
public Remote3Frame(@NotNull JsonObject obj) {
3941
playbackId = Utils.optString(obj, "playback_id", null);
@@ -56,20 +58,15 @@ public Remote3Frame(@NotNull JsonObject obj) {
5658
options = Options.opt(obj, "options");
5759
value = obj.getAsJsonPrimitive("value");
5860
track = Remote3Track.opt(obj, "track");
61+
prevTracks = Remote3Track.optArray(obj, "prev_tracks");
62+
nextTracks = Remote3Track.optArray(obj, "next_tracks");
5963
}
6064

6165

6266
public enum Endpoint {
63-
Play("play"),
64-
Pause("pause"),
65-
Resume("resume"),
66-
SeekTo("seek_to"),
67-
SkipNext("skip_next"),
68-
SkipPrev("skip_prev"),
69-
SetShufflingContext("set_shuffling_context"),
70-
SetRepeatingContext("set_repeating_context"),
71-
SetRepeatingTrack("set_repeating_track"),
72-
UpdateContext("update_context");
67+
Play("play"), Pause("pause"), Resume("resume"), SeekTo("seek_to"), SkipNext("skip_next"),
68+
SkipPrev("skip_prev"), SetShufflingContext("set_shuffling_context"), SetRepeatingContext("set_repeating_context"),
69+
SetRepeatingTrack("set_repeating_track"), UpdateContext("update_context"), SetQueue("set_queue");
7370

7471
private final String val;
7572

core/src/main/java/xyz/gianlu/librespot/player/remote/Remote3Track.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package xyz.gianlu.librespot.player.remote;
22

3+
import com.google.gson.JsonArray;
34
import com.google.gson.JsonElement;
45
import com.google.gson.JsonObject;
56
import com.google.protobuf.ByteString;
@@ -9,6 +10,10 @@
910
import xyz.gianlu.librespot.common.proto.Spirc;
1011
import xyz.gianlu.librespot.mercury.model.PlayableId;
1112

13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Objects;
16+
1217
/**
1318
* @author Gianlu
1419
*/
@@ -31,9 +36,44 @@ public static Remote3Track opt(@NotNull JsonObject obj, @NotNull String key) {
3136
return new Remote3Track(elm.getAsJsonObject());
3237
}
3338

34-
@NotNull
39+
@Nullable
40+
public static List<Remote3Track> optArray(@NotNull JsonObject obj, @NotNull String key) {
41+
JsonElement elm = obj.get(key);
42+
if (elm == null || !elm.isJsonArray()) return null;
43+
JsonArray array = elm.getAsJsonArray();
44+
45+
List<Remote3Track> list = new ArrayList<>(array.size());
46+
for (JsonElement track : array)
47+
list.add(new Remote3Track(track.getAsJsonObject()));
48+
49+
return list;
50+
}
51+
52+
public void addToState(@NotNull Spirc.State.Builder builder) {
53+
Spirc.TrackRef ref = toTrackRef();
54+
if (ref != null) builder.addTrack(ref);
55+
}
56+
57+
public void addTo(@NotNull List<Spirc.TrackRef> list) {
58+
Spirc.TrackRef ref = toTrackRef();
59+
if (ref != null) list.add(ref);
60+
}
61+
62+
@Nullable
3563
public Spirc.TrackRef toTrackRef() {
36-
return Spirc.TrackRef.newBuilder().setGid(ByteString.copyFrom(id().getGid())).setUri(uri).build();
64+
if (Objects.equals(uri, "spotify:delimiter"))
65+
return null;
66+
67+
boolean isQueued = false;
68+
if (metadata != null) {
69+
JsonElement elm = metadata.get("is_queued");
70+
if (elm != null) isQueued = elm.getAsBoolean();
71+
}
72+
73+
return Spirc.TrackRef.newBuilder()
74+
.setQueued(isQueued)
75+
.setGid(ByteString.copyFrom(id().getGid()))
76+
.setUri(uri).build();
3777
}
3878

3979
@NotNull

core/src/main/java/xyz/gianlu/librespot/player/tracks/PlaylistProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void unshuffleTracks() {
8686
int count = 80;
8787
for (Remote3Track track : tracks) {
8888
if (add || track.uri.equals(currentTrackUri)) {
89-
rebuildState.add(track.toTrackRef());
89+
track.addTo(rebuildState);
9090

9191
add = true;
9292
count--;

0 commit comments

Comments
 (0)