Skip to content

Commit 0f080de

Browse files
brnolddevgianlu
andauthored
Added play pause toggle command to the API (#244)
* Added play pause to the api * Added play pause to the api * Refactored + removed useless method Co-authored-by: Gianlu <[email protected]>
1 parent aa8667b commit 0f080de

4 files changed

Lines changed: 12 additions & 7 deletions

File tree

api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All the endpoints will respond with `200` if successful or:
1111

1212
### Player
1313
- `POST /player/load` Load a track from a given URI. The request body should contain two parameters: `uri` and `play`.
14+
- `POST /player/play-pause` Toggle play/pause status. Useful when using a remote.
1415
- `POST /player/pause` Pause playback.
1516
- `POST /player/resume` Resume playback.
1617
- `POST /player/next` Skip to next track.

api/src/main/java/xyz/gianlu/librespot/api/handlers/PlayerHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
179179
case LOAD:
180180
load(exchange, session, Utils.getFirstString(params, "uri"), Utils.getFirstBoolean(params, "play"));
181181
return;
182+
case PLAY_PAUSE:
183+
session.player().playPause();
184+
return;
182185
case PAUSE:
183186
session.player().pause();
184187
return;
@@ -209,7 +212,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
209212
}
210213

211214
private enum Command {
212-
LOAD("load"), PAUSE("pause"), RESUME("resume"), TRACKS("tracks"),
215+
LOAD("load"), PLAY_PAUSE("play-pause"), PAUSE("pause"), RESUME("resume"), TRACKS("tracks"),
213216
NEXT("next"), PREV("prev"), SET_VOLUME("set-volume"), SEEK("seek"),
214217
VOLUME_UP("volume-up"), VOLUME_DOWN("volume-down"), CURRENT("current"),
215218
ADD_TO_QUEUE("addToQueue"), REMOVE_FROM_QUEUE("removeFromQueue");

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public void play() {
110110
handleResume();
111111
}
112112

113+
public void playPause() {
114+
if (state.isPaused()) handleResume();
115+
else handlePause();
116+
}
117+
113118
public void pause() {
114119
handlePause();
115120
}
@@ -362,7 +367,7 @@ private void handleResume() {
362367
}
363368

364369
private void handlePause() {
365-
if (state.isPlaying()) {
370+
if (!state.isPaused()) {
366371
state.setState(true, true, false);
367372
sink.pause(false);
368373

@@ -536,7 +541,7 @@ private void endMetrics(String playbackId, @NotNull PlaybackMetrics.Reason reaso
536541

537542
@Override
538543
public void startedLoading() {
539-
if (state.isPlaying()) {
544+
if (!state.isPaused()) {
540545
state.setBuffering(true);
541546
state.updated();
542547
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ synchronized void setState(boolean playing, boolean paused, boolean buffering) {
133133
setPosition(state.getPositionAsOfTimestamp());
134134
}
135135

136-
boolean isPlaying() {
137-
return state.getIsPlaying() && !state.getIsPaused();
138-
}
139-
140136
boolean isPaused() {
141137
return state.getIsPlaying() && state.getIsPaused();
142138
}

0 commit comments

Comments
 (0)