-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicplay.html
73 lines (68 loc) · 2.15 KB
/
musicplay.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Music with Volume Control</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.music-control {
text-align: center;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
.volume-slider {
width: 200px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="music-control">
<button id="musicButton" onclick="toggleMusic()">Pause Music</button>
<br>
<label for="volumeControl">Volume:</label>
<input type="range" id="volumeControl" class="volume-slider" min="0" max="1" step="0.01" value="0.5" oninput="adjustVolume(this.value)">
</div>
<audio id="backgroundMusic" loop autoplay>
<source src="music/EmotionalPiano.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script>
const backgroundMusic = document.getElementById('backgroundMusic');
const musicButton = document.getElementById('musicButton');
const volumeControl = document.getElementById('volumeControl');
let isPlaying = true;
// Set initial volume
backgroundMusic.volume = 0.5;
// Toggle music play/pause
function toggleMusic() {
if (isPlaying) {
backgroundMusic.pause();
musicButton.textContent = 'Play Music';
} else {
backgroundMusic.play();
musicButton.textContent = 'Pause Music';
}
isPlaying = !isPlaying;
}
// Adjust volume
function adjustVolume(volume) {
backgroundMusic.volume = volume;
}
</script>
</body>
</html>