【问题标题】:multiple audio, auto stop other when current is playing with javascript多个音频,当当前正在使用 javascript 播放时自动停止其他
【发布时间】:2017-02-17 12:14:08
【问题描述】:

我在一个 html5 页面上有超过 5 个带有audioplayer.js 的音频播放器。

有没有人有一个简单的js脚本来在当前播放器播放时暂停所有其他播放器?

jQuery:

$( function(){
   $("audio").audioPlayer();
});

图书馆

https://osvaldas.info/examples/audio-player-responsive-and-touch-friendly/audioplayer.js

我试过如下

window.player = $('audio')[0];
$('.audioplayer-playpause').click(function () {
    if (player.paused) {
        player.play();
    } else {
        player.pause();
    }
});

我做了一个 jsFiddle 只是为了缓解事情,下面是链接:

JS Fiddle for Audio Player

【问题讨论】:

    标签: javascript jquery html audio audio-player


    【解决方案1】:

    试试下面的代码。我已经在 J​​S Fiddle 中对其进行了测试,它正在工作:) :)

    更新JSFiddle

    将此代码放入 Javascript 并删除您的代码。

    document.addEventListener('play', function(e){
        // get all <audio> tag elements in the page.
        var allAudios = document.getElementsByTagName('audio');
        // Iterate through all players and pause them, except for
        // the one who fired the "play" event ("target")
        for(var i = 0; i<allAudios.length; i++){
            if(allAudios[i] != e.target){
                allAudios[i].pause();
            }
        }
    }, true);

    【讨论】:

    • Noooo.. 你已经删除了插件初始化。请不要修改播放器代码
    • 我已经知道这个代码了.. 这在这个播放器上不起作用
    • 为什么要用window作为作用域?
    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2011-10-06
    • 2014-06-10
    相关资源
    最近更新 更多