-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.js
84 lines (70 loc) · 3.23 KB
/
audio.js
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
// Audio extension, https://github.com/schulle4u/yellow-audio
document.addEventListener("DOMContentLoaded", function() {
var audioLists = document.getElementsByClassName("audiolist");
Array.prototype.forEach.call(audioLists, function(container) {
var audioLinks = container.getElementsByTagName("a");
// Make every link an audio link
Array.prototype.forEach.call(audioLinks, function(link) {
link.setAttribute("data-role", "audio-link");
});
var playPauseButton = container.querySelector('[data-role="playPauseButton"]');
var stopButton = container.querySelector('[data-role="stopButton"]');
var rewindButton = container.querySelector('[data-role="rewindButton"]');
var forwardButton = container.querySelector('[data-role="forwardButton"]');
var volumeControl = container.querySelector('[data-role="volumeControl"]');
var speedControl = container.querySelector('[data-role="speedControl"]');
var currentAudio = null;
Array.prototype.forEach.call(audioLinks, function(link) {
link.addEventListener("click", function(event) {
event.preventDefault();
var audioFile = this.getAttribute("href");
if (currentAudio !== null) {
currentAudio.pause();
}
currentAudio = new Audio(audioFile);
currentAudio.volume = volumeControl.value;
currentAudio.playbackRate = speedControl.value;
currentAudio.preload = "none";
currentAudio.play();
});
});
playPauseButton.addEventListener("click", function() {
if (currentAudio !== null) {
if (currentAudio.paused) {
currentAudio.play();
playPauseButton.textContent = playPauseButton.getAttribute("data-pauseLabel");
} else {
currentAudio.pause();
playPauseButton.textContent = playPauseButton.getAttribute("data-playLabel");
}
}
});
stopButton.addEventListener("click", function() {
if (currentAudio !== null) {
currentAudio.pause();
currentAudio.currentTime = 0;
playPauseButton.textContent = playPauseButton.getAttribute("data-playLabel");
}
});
rewindButton.addEventListener("click", function() {
if (currentAudio !== null) {
currentAudio.currentTime -= 5; // Zurückspulen um 5 Sekunden (kann angepasst werden)
}
});
forwardButton.addEventListener("click", function() {
if (currentAudio !== null) {
currentAudio.currentTime += 5; // Vorwärtsspulen um 5 Sekunden (kann angepasst werden)
}
});
volumeControl.addEventListener("input", function() {
if (currentAudio !== null) {
currentAudio.volume = volumeControl.value;
}
});
speedControl.addEventListener("input", function() {
if (currentAudio !== null) {
currentAudio.playbackRate = speedControl.value;
}
});
});
});