This repository was archived by the owner on Oct 5, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbg-audio.html
More file actions
68 lines (59 loc) · 2 KB
/
Copy pathbg-audio.html
File metadata and controls
68 lines (59 loc) · 2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Background Audio</title>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
gap: 10px; /* Space between the audio player and button */
}
audio {
height: 40px; /* Adjust audio size */
}
button {
padding: 5px 10px;
font-size: 14px;
}
</style>
</head>
<body>
<audio id="bgAudio" controls autoplay muted>
<source src="audio/yinantiro-bluelantern.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<button id="loopToggle">Toggle Loop</button>
<script>
const audio = document.getElementById('bgAudio');
const loopToggle = document.getElementById('loopToggle');
// Set volume to 40%
audio.volume = 0.4;
// Retrieve saved time from localStorage (if any)
const savedTime = localStorage.getItem('audioTime');
if (savedTime) {
audio.currentTime = savedTime; // Set the current time of the audio player
}
// Set initial loop state based on user input
loopToggle.addEventListener('click', function() {
audio.loop = !audio.loop;
loopToggle.textContent = audio.loop ? 'Looping: On' : 'Looping: Off';
});
// Save the audio time to localStorage every time it updates
audio.addEventListener('timeupdate', function() {
localStorage.setItem('audioTime', audio.currentTime);
});
// Unmute the audio after the user clicks anywhere on the page
window.addEventListener('click', function() {
if (audio.muted) {
audio.muted = false;
}
});
</script>
</body>
</html>