暂停除了激活的所有其他玩家.用于 <audio> 的 jQuery 音频插件元素 [英] Pause all other players besides activated one. jQuery audio plugin for <audio> element

查看:24
本文介绍了暂停除了激活的所有其他玩家.用于 <audio> 的 jQuery 音频插件元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个插件,它对 html5 <audio> 元素应用不同的样式,并为不支持 .mp3 文件格式的浏览器提供 flash 回退.我遇到的问题是,一旦你开始播放一首歌曲,然后点击播放任何其他歌曲,所有歌曲都会同时播放,这是不好的用户体验.

I've got this plugin which applies different style to html5 <audio> element and provides flash fallback to browsers that don't support .mp3 file format. Problem I encountered is that once you start playing one song, and then click play on any other song, all of them will play at the same time, which is bad user experience.

我试图弄清楚如何编辑插件以使其一次只播放一首歌曲,所以假设用户听了一首歌曲然后点击播放另一首歌曲,第一个应该暂停,依此类推.我不完全确定,但我认为需要按照以下方式进行

I was trying to figure out how to edit plugin to make it only play one song at a time, so say if user listens to one song and then click play on other, first one should pause, and so on. I'm not entirely sure, but I think something need's to be edited along these lines:

    playPause: function() {
      if (this.playing) this.pause();
      else this.play();
    },

然而,插件中有很多地方似乎处理播放和暂停歌曲,所以我不确定这是完全正确的行.我对 jQuery 还是很陌生,无法完全弄清楚这个大型音频库是如何工作的,我一直在阅读代码注释,但仍然无法弄清楚.

插件的完整代码可在此处获得:http://freshbeer.lv/mg/js/audio.min.js

官方插件网站:http://kolber.github.io/audiojs/

您可以访问我的演示页面查看问题,只需单击多个播放器上的播放,您将看到它们都在同时播放:http://freshbeer.lv/mg/index.html

However, there are various places in the plugin which seem to deal with playing and pausing song, so I'm not sure that this is exact correct line for this. I'm still very new to jQuery, and can't entirely figure out how this big audio library works, I've been reading through code comment's , but still am unable to figure this out.

Full code of the plugin is available here: http://freshbeer.lv/mg/js/audio.min.js

Official plugin website: http://kolber.github.io/audiojs/

You can visit my demo page to see the issue, just click play on several players and you will see that they all play at the same time: http://freshbeer.lv/mg/index.html

推荐答案

使用 addEventListener 和 play 事件.这会暂停所有播放器,除了已按下播放按钮的播放器.

Use the addEventListener and the play event. This pauses all players except the one that the play button has been pressed on.

        audiojs.events.ready(function () {
            var as = audiojs.createAll();

            $('audio').each(function () {
                var myAudio = this;
                this.addEventListener('play', function () {
                    $('audio').each(function () {
                        if (!(this === myAudio)) {
                            this.pause();
                        }
                    });
                });
            });
        });

这篇关于暂停除了激活的所有其他玩家.用于 &lt;audio&gt; 的 jQuery 音频插件元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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