我在页面上有一个 html5 视频和一个我想用作背景音乐的音频文件。是否可以将音频控件绑定到视频控件(静音、音量、播放、暂停等)。音频不能成为视频文件的一部分,因为视频暂停,我仍然希望播放和循环播放背景音乐。
<video id="video" data-setup="{}" >
<source src="video/MyVideo.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
<source src="video/MyVideo.webmhd.webm" type='video/webm; codecs="vp8, vorbis"'>
<source src="video/MyVideo.oggtheora.ogv" type='video/ogg; codecs="theora, vorbis"' />
<p>Your browser doesn't support HTML5. Maybe you should upgrade.</p>
</video>
<audio id="music" loop="loop">
<source src="audio/background.ogg" type="audio/ogg" />
<source src="audio/background.mp3" type="audio/mpeg" />
<source src="audio/background.wav" type="audio/wav" />
<p> Your browser doesn't support HTML5. Maybe you should upgrade.</p>
</audio>
<input type="button" value="PLAY" id="playpause" onclick="playOrPause()" class="button_play">
<input type="button" value="Mute" id="mutebutton" onclick="muteOrUnmute()" class="button_play">
<input type="button" value="+" onclick="document.getElementById('video').volume+=0.2" class="button">
<input type="button" value="-" onclick="document.getElementById('video').volume-=0.2" class="button">
和一些 JavaScript
function playOrPause() {
if (video.ended || video.paused) {
video.play();
document.getElementById("playpause").value="PAUSE";
} else {
video.pause();
document.getElementById("playpause").value="PLAY";
}
}
var m = document.getElementById('mutebutton')[0];
video.onvolumechange = function(e) {
mutebutton.value = video.muted ? 'Unmute' : 'Mute';
}
function muteOrUnmute() {
if (video.muted) {
video.muted = false;
document.getElementById("mutebutton").value="Mute";
} else {
video.muted = true
document.getElementById("mutebutton").value="Unmute";
}
return false;
}
我是 JavaScript 和 html5 的新手,因此我们将不胜感激。