播放/暂停按钮HTML5音频

时间:2014-06-30 20:43:07

标签: javascript jquery html audio html5-audio

我正在尝试让HTML5音频在一个按钮中播放/暂停。我怎么会这样做呢?所以播放按钮切换到暂停图标,字体很棒'fa fa-pause'代码在这里:

<audio id="myTune">
<source src="http://96.47.236.72:8364/;">
</audio>
<div class="btn-group btn-group-xs">
<a href="javascript:void(0)" class="btn btn-default" data-toggle="tooltip" title="Preview"     onclick="document.getElementById('myTune').play()"><i class="fa fa-play"></i></a>

谢谢!

3 个答案:

答案 0 :(得分:6)

您可以在<i>标记中添加ID,并在状态更改时分配fa fa-pause类:

<a href="javascript:void(0)" class="btn btn-default" data-toggle="tooltip" title="Preview" onclick="aud_play_pause()"><i id="stateicon" class="fa fa-play"></i></a>

<script>
  function aud_play_pause() {
    var myAudio = document.getElementById("myTune");
    if (myAudio.paused) {
      $('#stateicon').removeClass('fa fa-play');
      $('#stateicon').addClass('fa fa-pause');
      myAudio.play();
    } else {
      $('#stateicon').removeClass('fa fa-pause');
      $('#stateicon').addClass('fa fa-play');
      myAudio.pause();
   }
 }

希望这有帮助

答案 1 :(得分:3)

给它一个旋转:

&#13;
&#13;
function aud_play_pause() {
  var myAudio = document.getElementById("myTune");
  if (myAudio.paused) {
    myAudio.play();
  } else {
    myAudio.pause();
  }
}
&#13;
<audio id="myTune" src="http://www.rachelgallen.com/HappyBirthday.mp3"></audio>


<button type="button"  onclick="aud_play_pause()">Play/Pause</button>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

此处您有多个播放器实例的版本

<强> HTML

<a href="javascript:void(0)" onclick="aud_play_pause(this)">
    <i class="control icon-play"></i>
    <audio class="xnine-player" src="/path/to/file#1.mp3" preload="auto"></audio>
</a>

<a href="javascript:void(0)" onclick="aud_play_pause(this)">
    <i class="control icon-play"></i>
    <audio class="xnine-player" src="/path/to/file#2.mp3" preload="auto"></audio>
</a>

<强> JAVASCRIPT

<script>
    function aud_play_pause(object) {
        var myAudio = object.querySelector(".xnine-player");
        var myIcon = object.querySelector(".control");
        if (myAudio.paused) {
            myIcon.className = "control icon-pause";
            myAudio.play();
        } else {
            myIcon.className = "control icon-play";
            myAudio.pause();
        }
    }
</script>

...