-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (53 loc) · 1.75 KB
/
script.js
File metadata and controls
73 lines (53 loc) · 1.75 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
function chooseFile() {
const input = document.createElement('input');
input.type = 'file';
input.onchange = function (e) {
const file = e.target.files[0];
loadToPlayer(file)
}
input.click();
}
function processLink() {
const link = document.getElementById("linkInput").value;
const player = document.getElementById('player');
player.src = link;
player.load();
}
function setSongInfo(fileContent) {
const mp3Tags = new MP3Tag(fileContent);
mp3Tags.read()
const { v2: {APIC, TIT2 : title, TPE1 : artist}} = mp3Tags.tags;
const coverBytes = APIC[0].data;
const coverURL = "data:image/png;base64," + btoa(String.fromCharCode.apply(null, new Uint8Array(coverBytes)));
console.log(title,artist)
document.getElementById("song").textContent = title || "Unknown Title";
document.getElementById("artist").textContent = artist || "Unknown Artist";
document.getElementById("cover").src = coverURL || "https://placehold.co/200";
}
function loadToPlayer(file) {
const player = document.getElementById('player');
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target.result;
setSongInfo(content);
}
reader.readAsArrayBuffer(file)
player.src = URL.createObjectURL(file);
player.load();
}
function initDropzone() {
const dropzone = document.getElementById('dropzone');
dropzone.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
})
dropzone.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
const file = e.dataTransfer.files[0];
loadToPlayer(file);
})
}
window.onload = () => {
initDropzone();
}