0

我在页面上有一个 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 的新手,因此我们将不胜感激。

4

1 回答 1

0

好的,所以我为我的音乐制作了一个变量

var a =document.getElementById("music");

并将我添加的播放功能连接在一起。a.play()到我的播放按钮和静音/取消静音按钮我这样做了

function muteOrUnmute() {
        if (video.muted) {
            video.muted = false;
            a.muted = false;
            document.getElementById("mutebutton").value="Mute";
        } else {
            video.muted = true;
            a.muted = true;
            document.getElementById("mutebutton").value="Unmute";
                }   
                return false;
        }

对于音量上下控制,我这样做了:

    <input type="button" value="+" onclick="document.getElementById('video').volume+=0.2; document.getElementById('music').volume+=0.1" class="button">
    <input type="button" value="-" onclick="document.getElementById('video').volume-=0.2; document.getElementById('music').volume-=0.1" class="button">
于 2013-01-14T18:11:44.987 回答