Skip to content

Commit 6a2679d

Browse files
committed
Send 'pfls' event when changing track, seeking and pausing + minor refactoring (#325)
1 parent c9eec6a commit 6a2679d

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,10 @@ private void sendVolume(int value) {
988988
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PVOL, volData);
989989
}
990990

991+
private void sendPipeFlush() {
992+
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_PFLS);
993+
}
994+
991995
void playbackEnded() {
992996
for (EventsListener l : new ArrayList<>(listeners))
993997
executorService.execute(l::onPlaybackEnded);
@@ -997,12 +1001,15 @@ void playbackPaused() {
9971001
long trackTime = state.getPosition();
9981002
for (EventsListener l : new ArrayList<>(listeners))
9991003
executorService.execute(() -> l.onPlaybackPaused(trackTime));
1004+
1005+
if (metadataPipe.enabled()) executorService.execute(this::sendPipeFlush);
10001006
}
10011007

10021008
void playbackResumed() {
10031009
long trackTime = state.getPosition();
10041010
for (EventsListener l : new ArrayList<>(listeners))
10051011
executorService.execute(() -> l.onPlaybackResumed(trackTime));
1012+
10061013
if (metadataPipe.enabled()) {
10071014
executorService.execute(() -> {
10081015
sendTrackInfo();
@@ -1027,13 +1034,20 @@ void trackChanged() {
10271034
TrackOrEpisode metadata = currentMetadata();
10281035
for (EventsListener l : new ArrayList<>(listeners))
10291036
executorService.execute(() -> l.onTrackChanged(id, metadata));
1037+
1038+
if (metadataPipe.enabled()) executorService.execute(this::sendPipeFlush);
10301039
}
10311040

10321041
void seeked(int pos) {
10331042
for (EventsListener l : new ArrayList<>(listeners))
10341043
executorService.execute(() -> l.onTrackSeeked(pos));
10351044

1036-
if (metadataPipe.enabled()) executorService.execute(this::sendProgress);
1045+
if (metadataPipe.enabled()) {
1046+
executorService.execute(() -> {
1047+
sendPipeFlush();
1048+
sendProgress();
1049+
});
1050+
}
10371051
}
10381052

10391053
void volumeChanged(@Range(from = 0, to = Player.VOLUME_MAX) int value) {

player/src/main/java/xyz/gianlu/librespot/player/metadata/MetadataPipe.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public final class MetadataPipe {
2626
public static final String CODE_PVOL = "70766f6c";
2727
public static final String CODE_PRGR = "70726772";
2828
public static final String CODE_PICT = "50494354";
29+
public static final String CODE_PFLS = "70666C73";
2930
private static final Logger LOGGER = LogManager.getLogger(MetadataPipe.class);
3031
private final File file;
3132
private FileOutputStream out;
@@ -34,15 +35,12 @@ public MetadataPipe(@NotNull PlayerConfiguration conf) {
3435
file = conf.metadataPipe;
3536
}
3637

37-
public void safeSend(@NotNull String type, @NotNull String code, @Nullable String payload) {
38-
if (!enabled())
39-
return;
38+
public void safeSend(@NotNull String type, @NotNull String code) {
39+
safeSend(type, code, (String) null);
40+
}
4041

41-
try {
42-
send(type, code, payload == null ? null : payload.getBytes(StandardCharsets.UTF_8));
43-
} catch (IOException ex) {
44-
LOGGER.error("Failed sending metadata through pipe!", ex);
45-
}
42+
public void safeSend(@NotNull String type, @NotNull String code, @Nullable String payload) {
43+
safeSend(type, code, payload == null ? null : payload.getBytes(StandardCharsets.UTF_8));
4644
}
4745

4846
public void safeSend(@NotNull String type, @NotNull String code, @Nullable byte[] payload) {
@@ -60,7 +58,7 @@ private synchronized void send(@NotNull String type, @NotNull String code, @Null
6058
if (file == null) return;
6159
if (out == null) out = new FileOutputStream(file);
6260

63-
if (payload != null) {
61+
if (payload != null && payload.length > 0) {
6462
out.write(String.format("<item><type>%s</type><code>%s</code><length>%d</length>\n<data encoding=\"base64\">%s</data></item>\n", type, code,
6563
payload.length, new String(Base64.getEncoder().encode(payload), StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
6664
} else {

0 commit comments

Comments
 (0)