启动其中一个音频文件同时播放两个时静音

Start one of the audio files muted when playing two at the same time

我有两个音频文件同时启动,还有一个切换按钮可以通过将另一个静音来在两个文件之间切换。

一切都如我所愿,但当我按下播放键时,我希望 audio2 开始静音。现在你可以同时听到它们。

https://jsfiddle.net/9z1dfm4p/

var audio1 = document.getElementById('audio1');
var audio2 = document.getElementById('audio2');
var button1 = document.getElementById('button1');
var isPlaying = false;
var muteToggle = true;
var play = function() {
  if (!isPlaying) {
    button1.innerHTML = 'Pause';
    audio1.play(); // this stream will immediately stop when the next line is run on iOS
    audio2.play(); // this will stop audio1 on iOS
    isPlaying = true;
  } else {
    button1.innerHTML = 'Play';
    audio1.pause(); // this stream will immediately stop when the next line is run on iOS
    audio2.pause(); // this will stop audio1 on iOS
    isPlaying = false;
  }
}

var mute = function() {
  if (muteToggle) {
    muteToggle = false;
    audio1.muted = true;
    audio2.muted = false;
  } else {
    muteToggle = true;
    audio2.muted = true;
    audio1.muted = false;
  }
}
<audio id="audio1">
    <source src="http://jplayer.org/audio/mp3/Miaow-07-Bubble.mp3" type="audio/mpeg" />
    </audio><br />
<audio id="audio2">
    <source src="https://allthingsaudio.wikispaces.com/file/view/Shuffle%20for%20K.M.mp3/139190697/Shuffle%20for%20K.M.mp3" type="audio/mpeg"/>
    </audio><br />
<button onclick="play();" id="button1">Play</button><br />
<button onclick="mute();" id="button2">Toggle</button><br />

您可以将第二个<audio>设置为从头开始静音。

<audio id="audio2" muted="true">

https://jsfiddle.net/9z1dfm4p/2/

编辑:清理代码并更好地显示正在播放的歌曲。

https://jsfiddle.net/9z1dfm4p/7/