-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
131 lines (115 loc) · 3.94 KB
/
index.html
File metadata and controls
131 lines (115 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Deezer QR Scanner</title>
<script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script>
<style>
body,
html {
margin: 0;
padding: 0;
background: #111;
text-align: center;
color: white;
font-family: sans-serif;
height: 100vh;
overflow: hidden;
}
#scanner,
/* #player {
position: absolute;
inset: 0;
display: none;
text-align: center;
align-items: center;
justify-content: center;
flex-direction: column;
} */
/* #scanner.active,
#player.active {
display: flex;
} */
/*
#qr-reader {
width: 100%;
max-width: 400px;
} */
audio {
margin-top: 1em;
}
</style>
</head>
<body>
<h1>QR-Song Game</h1>
<div id="qr-reader"></div>
<p id="status">Scanne einen QR-Code…</p>
<audio id="audio" controls autoplay></audio>
<script>
const statusElement = document.getElementById('status');
const audioElement = document.getElementById('audio');
function extractDeezerTrackId(url) {
const match = url.match(/deezer\.com\/track\/(\d+)/);
console.log(match)
const match_spotify = url.match(/(?:spotify:track:|#)(\d+)$/);
console.log(match_spotify);
if (match) {
console.log("hey")
return match[1]
};
if (match_spotify) {
console.log("ho")
return match_spotify[1]
};
return null;
}
function fetchDeezerJsonp(trackId) {
const callbackName = `deezerCallback_${trackId}`;
return new Promise((resolve, reject) => {
window[callbackName] = function (data) {
resolve(data);
delete window[callbackName];
script.remove();
};
const script = document.createElement('script');
script.src = `https://api.deezer.com/track/${trackId}?output=jsonp&callback=${callbackName}`;
script.onerror = () => reject(new Error("Script-Fehler bei Deezer-API"));
document.body.appendChild(script);
});
}
async function handleScanSuccess(decodedText) {
// if (!decodedText.includes("deezer.com/track/")) {
// statusElement.textContent = "Kein gültiger Deezer-Link.";
// return;
// }
const trackId = extractDeezerTrackId(decodedText);
if (!trackId) {
statusElement.textContent = "Track-ID konnte nicht extrahiert werden.";
return;
}
statusElement.textContent = `Lade Track #${trackId}…`;
try {
const trackData = await fetchDeezerJsonp(trackId);
if (trackData.preview) {
audioElement.src = trackData.preview;
audioElement.play();
// statusElement.textContent = `Spiele: ${trackData.title} – ${trackData.artist.name}`;
statusElement.textContent = "";
} else {
statusElement.textContent = "Keine Vorschau verfügbar.";
}
} catch (err) {
console.error(err);
statusElement.textContent = "Fehler beim Laden der Track-Daten.";
}
}
// QR-Scanner starten
new Html5Qrcode("qr-reader").start(
{ facingMode: "environment" },
{ fps: 10, qrbox: 800 },
handleScanSuccess,
(errorMessage) => { /* Fehler ignorieren */ }
);
</script>
</body>
</html>