Skip to content

Commit 751b79e

Browse files
funtaxma5309devgianlu
authored
Add EventsListener.onPanicState() (#233)
* Add EventsListener.onPanicState() * Removed reason + send WS event for `panic` Co-authored-by: ma5309 <[email protected]> Co-authored-by: devgianlu <[email protected]>
1 parent 60b1db8 commit 751b79e

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

api/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ All the endpoints will respond with `200` if successful or:
3737
### Events
3838
You can subscribe for players events by creating a WebSocket connection to `/events`.
3939
The currently available events are:
40-
- `contextChanged`, the Spotify context URI changed
41-
- `trackChanged`, the Spotify track URI changed
42-
- `playbackPaused`, playback has been paused
43-
- `playbackResumed`, playback has been resumed
44-
- `volumeChanged`, playback volume changed
45-
- `trackSeeked`, track has been seeked
46-
- `metadataAvailable`, metadata for the current track is available
47-
- `playbackHaltStateChanged`, playback halted or resumed from halt
48-
- `sessionCleared`, (Zeroconf only) current session went away
49-
- `sessionChanged`, (Zeroconf only) current session changed
50-
- `inactiveSession`, current session is now inactive (no audio)
51-
- `connectionDropped`, a network error occurred and we're trying to reconnect
52-
- `connectionEstablished`, successfully reconnected
40+
- `contextChanged` The Spotify context URI changed
41+
- `trackChanged` The Spotify track URI changed
42+
- `playbackPaused` Playback has been paused
43+
- `playbackResumed` Playback has been resumed
44+
- `volumeChanged` Playback volume changed
45+
- `trackSeeked` Track has been seeked
46+
- `metadataAvailable` Metadata for the current track is available
47+
- `playbackHaltStateChanged` Playback halted or resumed from halt
48+
- `sessionCleared` Current session went away (Zeroconf only)
49+
- `sessionChanged` Current session changed (Zeroconf only)
50+
- `inactiveSession` Current session is now inactive (no audio)
51+
- `connectionDropped` A network error occurred and we're trying to reconnect
52+
- `connectionEstablished` Successfully reconnected
53+
- `panic` Entered the panic state, playback is stopped. This is usually recoverable.
5354

5455
## Examples
5556
`curl -X POST -d "uri=spotify:track:xxxxxxxxxxxxxxxxxxxxxx&play=true" http://localhost:24879/player/load`

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public void onVolumeChanged(@Range(from = 0, to = 1) float volume) {
108108
dispatch(obj);
109109
}
110110

111+
@Override
112+
public void onPanicState() {
113+
JsonObject obj = new JsonObject();
114+
obj.addProperty("event", "panic");
115+
dispatch(obj);
116+
}
117+
111118
@Override
112119
public void onSessionCleared(@NotNull Session old) {
113120
old.player().removeEventsListener(this);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ private void panicState(@Nullable PlaybackMetrics.Reason reason) {
171171
} else if (playerSession != null) {
172172
endMetrics(playerSession.currentPlaybackId(), reason, playerSession.currentMetrics(), state.getPosition());
173173
}
174+
175+
events.panicState();
174176
}
175177

176178
/**
@@ -816,6 +818,8 @@ public interface EventsListener {
816818
void onInactiveSession(boolean timeout);
817819

818820
void onVolumeChanged(@Range(from = 0, to = 1) float volume);
821+
822+
void onPanicState();
819823
}
820824

821825
/**
@@ -1078,6 +1082,12 @@ void inactiveSession(boolean timeout) {
10781082
executorService.execute(() -> l.onInactiveSession(timeout));
10791083
}
10801084

1085+
1086+
private void panicState() {
1087+
for (EventsListener l : new ArrayList<>(listeners))
1088+
executorService.execute(l::onPanicState);
1089+
}
1090+
10811091
public void close() {
10821092
executorService.shutdown();
10831093
}

0 commit comments

Comments
 (0)