Play selected audio while pausing/resetting others

左心房为你撑大大i 提交于 2021-01-28 04:29:38

问题


I have two audio elements that play through a button's click event. I've successfully managed to pause one if another is selected but also need to set the paused element back to 0.0 seconds (i.e pause and reset).

I'm aware that Javascript currently doesn't have a stop() method which led assume that this would be done by setting its currentTime to 0. If so I just haven't been able to figure out the best way to incorporate this method in my code.

Right now I'm pausing all audio elements in the latter half of the conditional using $(".audio").trigger("pause"); which doesn't too efficient performance wise. What would be the best way to pause and reset only the previously played audio file and not every one on the page?

http://jsfiddle.net/txrcxfpy/


回答1:


use below code . check DEMO

 $(function() {    
  $('.track-button').click(function() {
    var reSet = $('.track-button').not($(this)).siblings(".audio").get(0);
    reSet.pause();
    reSet.currentTime = 0;

    var $this          = $(this),
        trackNum       = $this.text(),
        currentAudio   = $this.siblings(".audio"),
        audioIsPaused  = currentAudio.get(0).paused;

    if (audioIsPaused) {
        currentAudio.get(0).play();
    } else {
        currentAudio.get(0).pause();
    }
  });
});


来源:https://stackoverflow.com/questions/29689920/play-selected-audio-while-pausing-resetting-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!