在暂停/重设其他音频时播放选定的音频 [英] Play selected audio while pausing/resetting others

查看:43
本文介绍了在暂停/重设其他音频时播放选定的音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个通过按钮的click事件播放的音频元素.如果已选择另一个元素,我已经成功设法将其暂停,但还需要将已暂停的元素设置回0.0秒(即暂停和重置).

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

我知道Javascript当前没有 stop()方法,该方法导致假定将其 currentTime 设置为 0即可完成此操作.如果是这样,我只是无法找出将这种方法合并到我的代码中的最佳方法.

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.

现在我要使用 $(.audio").trigger("pause"); 在条件的后半部暂停所有音频元素不太明智地执行性能.什么是最好的方式来暂停和重置以前播放的音频文件,而不是页面上的每个音频文件?

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/

http://jsfiddle.net/txrcxfpy/

推荐答案

使用以下代码.检查 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();
    }
  });
});

这篇关于在暂停/重设其他音频时播放选定的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆