Skip to content

Commit fa08088

Browse files
committed
PlayerHandler methods
1 parent 05070a9 commit fa08088

6 files changed

Lines changed: 105 additions & 7 deletions

File tree

api-client/src/main/java/xyz.gianlu.librespot.api.client/MainController.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ public void clickedResponse(MouseEvent mouseEvent) throws IOException {
201201
stage.showAndWait();
202202
}
203203

204+
public void clickedPlayerPlay(MouseEvent event) {
205+
if (networkThread == null) return;
206+
networkThread.sendPlayer("play", this);
207+
}
208+
209+
public void clickedPlayerPause(MouseEvent event) {
210+
if (networkThread == null) return;
211+
networkThread.sendPlayer("pause", this);
212+
}
213+
214+
public void clickedPlayerPlayPause(MouseEvent event) {
215+
if (networkThread == null) return;
216+
networkThread.sendPlayer("playPause", this);
217+
}
218+
219+
public void clickedPlayerNext(MouseEvent event) {
220+
if (networkThread == null) return;
221+
networkThread.sendPlayer("next", this);
222+
}
223+
224+
public void clickedPlayerPrev(MouseEvent event) {
225+
if (networkThread == null) return;
226+
networkThread.sendPlayer("prev", this);
227+
}
228+
204229
public static class Header {
205230
private final SimpleStringProperty key;
206231
private final SimpleStringProperty value;

api-client/src/main/java/xyz.gianlu.librespot.api.client/NetworkThread.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ void sendGeneral(@NotNull String id, @NotNull String method, @Nullable String pa
6565
requests.put(id, listener);
6666
}
6767

68+
void sendPlayer(@NotNull String suffix, @NotNull Callback listener) {
69+
sendGeneral(String.valueOf(ThreadLocalRandom.current().nextInt(1000)), "player." + suffix, null, listener);
70+
}
71+
6872
void sendMercury(@NotNull String method, @NotNull String uri, @Nullable String contentType, @NotNull Map<String, String> headers, @NotNull Callback listener) {
6973
String id = String.valueOf(ThreadLocalRandom.current().nextInt(1000));
7074

api-client/src/main/resources/main.fxml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,42 @@
133133
</HBox>
134134
</content>
135135
</Tab>
136+
<Tab closable="false" text="Player">
137+
<content>
138+
<FlowPane>
139+
<children>
140+
<Button mnemonicParsing="false" onMouseClicked="#clickedPlayerPlay" text="Play">
141+
<FlowPane.margin>
142+
<Insets right="8.0"/>
143+
</FlowPane.margin>
144+
</Button>
145+
<Button layoutX="18.0" layoutY="18.0" onMouseClicked="#clickedPlayerPause"
146+
mnemonicParsing="false" text="Pause">
147+
<FlowPane.margin>
148+
<Insets right="8.0"/>
149+
</FlowPane.margin>
150+
</Button>
151+
<Button layoutX="132.0" layoutY="18.0" onMouseClicked="#clickedPlayerPlayPause"
152+
mnemonicParsing="false" text="Play/Pause">
153+
<FlowPane.margin>
154+
<Insets right="8.0"/>
155+
</FlowPane.margin>
156+
</Button>
157+
<Button layoutX="56.0" layoutY="18.0" onMouseClicked="#clickedPlayerNext"
158+
mnemonicParsing="false" text="Next">
159+
<FlowPane.margin>
160+
<Insets right="8.0"/>
161+
</FlowPane.margin>
162+
</Button>
163+
<Button layoutX="56.0" layoutY="18.0" onMouseClicked="#clickedPlayerPrev"
164+
mnemonicParsing="false" text="Previous"/>
165+
</children>
166+
<padding>
167+
<Insets bottom="8.0" left="8.0" right="8.0" top="8.0"/>
168+
</padding>
169+
</FlowPane>
170+
</content>
171+
</Tab>
136172
</tabs>
137173
</TabPane>
138174
</content>

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,25 @@ public PlayerHandler(@NotNull Session session) {
2222
protected @NotNull JsonElement handleRequest(ApiServer.@NotNull Request request) throws ApiServer.PredefinedJsonRpcException {
2323
switch (request.getSuffix()) {
2424
case "play":
25-
case "stop":
25+
player.play();
26+
break;
2627
case "pause":
28+
player.pause();
29+
break;
2730
case "next":
31+
player.next();
32+
break;
2833
case "prev":
34+
player.previous();
35+
break;
2936
case "playPause":
30-
return string("OK");
37+
player.playPause();
38+
break;
3139
default:
3240
throw ApiServer.PredefinedJsonRpcException.from(request, ApiServer.PredefinedJsonRpcError.METHOD_NOT_FOUND);
33-
3441
}
42+
43+
return string("OK");
3544
}
3645

3746
@Override

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import org.apache.log4j.Logger;
44
import org.jetbrains.annotations.NotNull;
55
import xyz.gianlu.librespot.common.Utils;
6-
import xyz.gianlu.librespot.core.Session;
76
import xyz.gianlu.librespot.common.proto.Metadata;
87
import xyz.gianlu.librespot.common.proto.Spirc;
8+
import xyz.gianlu.librespot.core.Session;
99
import xyz.gianlu.librespot.spirc.FrameListener;
1010
import xyz.gianlu.librespot.spirc.SpotifyIrc;
1111

@@ -48,6 +48,26 @@ public Player(@NotNull PlayerConfiguration conf, @NotNull CacheManager.CacheConf
4848
spirc.addListener(this);
4949
}
5050

51+
public void playPause() {
52+
handlePlayPause();
53+
}
54+
55+
public void play() {
56+
handlePlay();
57+
}
58+
59+
public void pause() {
60+
handlePause();
61+
}
62+
63+
public void next() {
64+
handleNext();
65+
}
66+
67+
public void previous() {
68+
handlePrev();
69+
}
70+
5171
@NotNull
5272
private Spirc.State.Builder initState() {
5373
return Spirc.State.newBuilder()
@@ -79,8 +99,7 @@ public void frame(@NotNull Spirc.Frame frame) {
7999
handlePause();
80100
break;
81101
case kMessageTypePlayPause:
82-
if (state.getStatus() == Spirc.PlayStatus.kPlayStatusPlay) handlePause();
83-
else if (state.getStatus() == Spirc.PlayStatus.kPlayStatusPause) handlePlay();
102+
handlePlayPause();
84103
break;
85104
case kMessageTypeNext:
86105
handleNext();
@@ -115,6 +134,11 @@ public void frame(@NotNull Spirc.Frame frame) {
115134
}
116135
}
117136

137+
private void handlePlayPause() {
138+
if (state.getStatus() == Spirc.PlayStatus.kPlayStatusPlay) handlePause();
139+
else if (state.getStatus() == Spirc.PlayStatus.kPlayStatusPause) handlePlay();
140+
}
141+
118142
private void handleSetVolume(int volume) {
119143
spirc.deviceState().setVolume(volume);
120144

mdnsjava

0 commit comments

Comments
 (0)