-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio.js
71 lines (56 loc) · 1.74 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
(function () {
"use strict";
let player;
let isPlaying = false;
const script = document.createElement("script");
script.src = "https://www.youtube.com/iframe_api";
document.head.appendChild(script);
window.onYouTubeIframeAPIReady = function () {
const playerElement = document.getElementById("player");
if (!playerElement) return;
const videoId = playerElement.getAttribute("data-video-id");
if (!videoId) return;
player = new YT.Player("player", {
height: "0",
width: "0",
videoId: videoId,
playerVars: {
autoplay: 0,
controls: 0,
},
events: {
onReady: initializeControls,
},
});
};
function initializeControls() {
const playButton = document.getElementById("play-pause-btn");
const volumeControl = document.getElementById("volume-control");
if (!playButton || !volumeControl) return;
playButton.addEventListener("click", togglePlayPause);
volumeControl.addEventListener("input", updateVolume);
updateVolume();
}
function togglePlayPause() {
if (!player) return;
const playButton = document.getElementById("play-pause-btn");
if (!playButton) return;
if (isPlaying) {
player.pauseVideo();
playButton.textContent = "Play";
} else {
player.playVideo();
playButton.textContent = "Pause";
}
isPlaying = !isPlaying;
}
function updateVolume() {
if (!player) return;
const volumeControl = document.getElementById("volume-control");
const volumeDisplay = document.getElementById("volume-display");
if (!volumeControl || !volumeDisplay) return;
const volume = volumeControl.value;
player.setVolume(volume);
volumeDisplay.textContent = volume + "%";
}
})();