playback in spotify!

pull/24/head
eric 2025-08-08 01:53:23 +02:00
parent 1d901c5222
commit 24eef746d7
2 changed files with 73 additions and 10 deletions

View File

@ -41,7 +41,16 @@ public class GameController {
app.get("/api/game/{gameId}/players", this::getPlayers); app.get("/api/game/{gameId}/players", this::getPlayers);
app.post("/api/game/{gameId}/start-round", this::startRound); app.post("/api/game/{gameId}/start-round", this::startRound);
app.post("/api/game/{gameId}/guess", this::guess); app.post("/api/game/{gameId}/guess", this::guess);
app.get("/api/spotify/devices", this::getDevices); app.post("/api/spotify/play", this::playTrack);
app.get("/api/spotify/devices", ctx -> {
String username = ctx.queryParam("username");
if (username == null) {
ctx.status(400).result("username fehlt");
return;
}
var devices = authService.getDevices(username); // Diese Methode muss es im SpotifyAuthService geben
ctx.json(Map.of("devices", devices));
});
} }
private void createGame(Context ctx) { private void createGame(Context ctx) {
@ -109,17 +118,42 @@ public class GameController {
"scores", game.scores() "scores", game.scores()
)); ));
} }
// Backend: Devices abrufen private void playTrack(Context ctx) {
private void getDevices(Context ctx) { Map<String, String> body = ctx.bodyAsClass(Map.class);
String username = ctx.queryParam("username"); String username = body.get("username");
if (username == null) { String deviceId = body.get("device_id");
ctx.status(400).result("username fehlt"); String trackUri = body.get("track_uri");
return; if (username == null || deviceId == null || trackUri == null) {
} ctx.status(400).result("Fehlende Parameter: username, device_id oder track_uri");
List<Map<String, Object>> devices = authService.getDevices(username); // Implementiere diese Methode im SpotifyAuthService return;
ctx.json(Map.of("devices", devices));
} }
try {
String accessToken = authService.getAccessTokenForUser(username); // Token vom SpotifyAuthService holen
OkHttpClient client = new OkHttpClient();
ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
jsonNode.putArray("uris").add(trackUri);
String jsonBody = jsonNode.toString();
Request request = 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()) {
ctx.status(204).result("Track erfolgreich abgespielt");
} else {
ctx.status(response.code()).result("Fehler: " + response.body().string());
}
}
} catch (Exception e) {
log.error("Fehler beim Abspielen des Tracks", e);
ctx.status(500).result("Interner Fehler");
}
}

View File

@ -192,3 +192,32 @@ function handleGameEnd({winner}) {
scoreboard.innerHTML = ""; scoreboard.innerHTML = "";
}, 6000); }, 6000);
} }
// public/js/game.js
async function playOnSpotify(trackUri, username) {
const deviceId = document.getElementById("deviceSelect")?.value;
if (!deviceId) {
alert("Bitte ein Wiedergabegerät auswählen!");
return;
}
try {
const response = await fetch("/api/spotify/play", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
username,
device_id: deviceId,
track_uri: trackUri
})
});
if (!response.ok) {
const error = await response.text();
alert(`Fehler beim Abspielen: ${error}`);
}
} catch (err) {
alert(`Netzwerkfehler: ${err.message}`);
}
}
window.playOnSpotify = playOnSpotify;