最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PlayPause Button HTML5 Audio - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get HTML5 Audio to play/pause in one button. How would I possibly go around doing this? So the play button switches to the pause icon which is font awesome 'fa fa-pause' The code is here:

<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>

Thank you!

I'm trying to get HTML5 Audio to play/pause in one button. How would I possibly go around doing this? So the play button switches to the pause icon which is font awesome 'fa fa-pause' The code is here:

<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>

Thank you!

Share Improve this question edited Feb 9, 2019 at 19:13 Rachel Gallen 28.6k22 gold badges75 silver badges86 bronze badges asked Jun 30, 2014 at 20:43 screamingchimpsscreamingchimps 2712 gold badges4 silver badges8 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

You can put an id to the <i> tag and assign the fa fa-pause class when change of state:

<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();
   }
 }

Hope this helps

Give this a whirl:

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


<button type="button"  onclick="aud_play_pause()">Play/Pause</button>

Here You have version for multiple instances of player

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>

...

发布评论

评论列表(0)

  1. 暂无评论