playback in spotify!
parent
1d901c5222
commit
24eef746d7
|
|
@ -41,7 +41,16 @@ public class GameController {
|
|||
app.get("/api/game/{gameId}/players", this::getPlayers);
|
||||
app.post("/api/game/{gameId}/start-round", this::startRound);
|
||||
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) {
|
||||
|
|
@ -109,17 +118,42 @@ public class GameController {
|
|||
"scores", game.scores()
|
||||
));
|
||||
}
|
||||
// Backend: Devices abrufen
|
||||
private void getDevices(Context ctx) {
|
||||
String username = ctx.queryParam("username");
|
||||
if (username == null) {
|
||||
ctx.status(400).result("username fehlt");
|
||||
return;
|
||||
}
|
||||
List<Map<String, Object>> devices = authService.getDevices(username); // Implementiere diese Methode im SpotifyAuthService
|
||||
ctx.json(Map.of("devices", devices));
|
||||
private void playTrack(Context ctx) {
|
||||
Map<String, String> body = ctx.bodyAsClass(Map.class);
|
||||
String username = body.get("username");
|
||||
String deviceId = body.get("device_id");
|
||||
String trackUri = body.get("track_uri");
|
||||
if (username == null || deviceId == null || trackUri == null) {
|
||||
ctx.status(400).result("Fehlende Parameter: username, device_id oder track_uri");
|
||||
return;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -192,3 +192,32 @@ function handleGameEnd({winner}) {
|
|||
scoreboard.innerHTML = "";
|
||||
}, 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;
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue