playback fängt in der mitte an
parent
24eef746d7
commit
789bc20106
|
|
@ -129,23 +129,42 @@ public class GameController {
|
|||
}
|
||||
|
||||
try {
|
||||
String accessToken = authService.getAccessTokenForUser(username); // Token vom SpotifyAuthService holen
|
||||
String accessToken = authService.getAccessTokenForUser(username);
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
// 1. Track-Details holen
|
||||
String trackId = trackUri.split(":")[2];
|
||||
Request getTrack = new Request.Builder()
|
||||
.url("https://api.spotify.com/v1/tracks/" + trackId)
|
||||
.addHeader("Authorization", "Bearer " + accessToken)
|
||||
.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;
|
||||
|
||||
// 2. Play-Request mit position_ms
|
||||
ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
|
||||
jsonNode.putArray("uris").add(trackUri);
|
||||
jsonNode.put("position_ms", startOffset);
|
||||
String jsonBody = jsonNode.toString();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
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 response = client.newCall(request).execute()) {
|
||||
if (response.isSuccessful()) {
|
||||
try (Response playResp = client.newCall(playReq).execute()) {
|
||||
if (playResp.isSuccessful()) {
|
||||
ctx.status(204).result("Track erfolgreich abgespielt");
|
||||
} else {
|
||||
ctx.status(response.code()).result("Fehler: " + response.body().string());
|
||||
ctx.status(playResp.code()).result("Fehler: " + playResp.body().string());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue