playback fängt in der mitte an
parent
24eef746d7
commit
789bc20106
|
|
@ -129,23 +129,42 @@ public class GameController {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String accessToken = authService.getAccessTokenForUser(username); // Token vom SpotifyAuthService holen
|
String accessToken = authService.getAccessTokenForUser(username);
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
|
|
||||||
jsonNode.putArray("uris").add(trackUri);
|
|
||||||
String jsonBody = jsonNode.toString();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
// 1. Track-Details holen
|
||||||
.url("https://api.spotify.com/v1/me/player/play?device_id=" + deviceId)
|
String trackId = trackUri.split(":")[2];
|
||||||
|
Request getTrack = new Request.Builder()
|
||||||
|
.url("https://api.spotify.com/v1/tracks/" + trackId)
|
||||||
.addHeader("Authorization", "Bearer " + accessToken)
|
.addHeader("Authorization", "Bearer " + accessToken)
|
||||||
.put(RequestBody.create(jsonBody, MediaType.parse("application/json")))
|
|
||||||
.build();
|
.build();
|
||||||
|
try (Response trackResp = client.newCall(getTrack).execute()) {
|
||||||
|
if (!trackResp.isSuccessful()) {
|
||||||
|
ctx.status(500).result("Fehler beim Laden der Track-Details");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var node = new com.fasterxml.jackson.databind.ObjectMapper().readTree(trackResp.body().string());
|
||||||
|
long durationMs = node.get("duration_ms").asLong();
|
||||||
|
long startOffset = durationMs / 2;
|
||||||
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
// 2. Play-Request mit position_ms
|
||||||
if (response.isSuccessful()) {
|
ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
|
||||||
ctx.status(204).result("Track erfolgreich abgespielt");
|
jsonNode.putArray("uris").add(trackUri);
|
||||||
} else {
|
jsonNode.put("position_ms", startOffset);
|
||||||
ctx.status(response.code()).result("Fehler: " + response.body().string());
|
String jsonBody = jsonNode.toString();
|
||||||
|
|
||||||
|
Request playReq = new Request.Builder()
|
||||||
|
.url("https://api.spotify.com/v1/me/player/play?device_id=" + deviceId)
|
||||||
|
.addHeader("Authorization", "Bearer " + accessToken)
|
||||||
|
.put(RequestBody.create(jsonBody, MediaType.parse("application/json")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response playResp = client.newCall(playReq).execute()) {
|
||||||
|
if (playResp.isSuccessful()) {
|
||||||
|
ctx.status(204).result("Track erfolgreich abgespielt");
|
||||||
|
} else {
|
||||||
|
ctx.status(playResp.code()).result("Fehler: " + playResp.body().string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue