【问题标题】:Play and pause one HTML5 audio element at a time via jQuery通过 jQuery 一次播放和暂停一个 HTML5 音频元素
【发布时间】:2011-08-08 23:19:22
【问题描述】:

我在一个页面上有几个 HTML5 音频元素,我正在使用 jQuery 来播放和暂停它们。播放和暂停功能可以正常工作,但可以同时播放所有曲目。

如何重写此代码,以便一次只能播放一首歌曲?也就是说..如果一个正在播放并且您点击另一个,暂停前一个并播放最近的点击。

谢谢!

HTML:

<div id="music_right">
        <div class="thumbnail" id="paparazzi">
            <a class="playback">
                <img class="play" src="http://www.lucisz.com/imgs/play.png" />
            </a>
            <audio>
             <source src="../audio/fernando_garibay_paparazzisnlmix.ogg" type="audio/ogg" />
                <source src="../audio/fernando_garibay_paparazzisnlmix.mp3" type="audio/mpeg" />
                Your browser does not support HTML5 audio.
            </audio>
        </div>
        <div class="thumbnail" id="danceinthedark">
            <a class="playback">
                <img class="play" src="http://www.lucisz.com/imgs/play.png" />
            </a>
            <audio>
             <source src="../audio/fernando_garibay_danceinthedark.ogg" type="audio/ogg" />
                <source src="../audio/fernando_garibay_danceinthedark.mp3" type="audio/mpeg" />
                Your browser does not support HTML5 audio.
            </audio>
        </div>
        <div class="thumbnail" id="bornthisway">
            <a class="playback">
                <img class="play" src="http://www.lucisz.com/imgs/play.png" />
            </a>
            <audio>
             <source src="../audio/fernando_garibay_bornthisway.ogg" type="audio/ogg" />
                <source src="../audio/fernando_garibay_bornthisway.mp3" type="audio/mpeg" />
                Your browser does not support HTML5 audio.
            </audio>
        </div>
    </div>

JavaScript:(有效,但单独播放/暂停)

$(function() {
    $(".playback").click(function(e) {
        e.preventDefault();
        var song = $(this).next('audio').get(0);
        if (song.paused)
            song.play();
        else
            song.pause();
    });
});

JavaScript:(我的丑陋概念)

$(function() {
    $(".playback").click(function(e) {
        e.preventDefault();
        var song = $(this).next('audio').get(0);
        if (song.paused)
            song.play();
            song.not($(this).pause();
        else
            song.pause();
    });
});

【问题讨论】:

  • 你试过什么?...没有人会为你写。好吧,大多数人就是这样。
  • @NeXXeuS。我听到你了。用粗略的语法尝试了一些不同的东西。 If/then 语句对我来说很难理解。我会将我的概念添加到问题中

标签: javascript jquery html audio playback


【解决方案1】:
var curPlaying;

$(function() {
    $(".playback").click(function(e) {
        e.preventDefault();
        var song = $(this).next('audio')[0];
        if(curPlaying) { $("audio", "#"+curPlaying)[0].pause(); }
        if(song.paused) { song.play(); } else { song.pause(); }
        curPlaying = $(this).parent()[0].id;
    });
});

应该可以的。

编辑:

var curPlaying;

$(function() {
    $(".playback").click(function(e) {
        e.preventDefault();
        var song = $(this).next('audio')[0];
        if(song.paused){
            song.play();
            if(curPlaying) $("audio", "#"+curPlaying)[0].pause();
        } else { song.pause(); }
        curPlaying = $(this).parent()[0].id;
    });
});

【讨论】:

  • @NeXXeuS 我爱你。您的功能在完美暂停其他活动歌曲的同时播放歌曲.. 但如果您再次单击它,则不再暂停正在播放的歌曲。
  • @NeXXeuS。我的错误,让我澄清一下。页面加载完毕,所有玩家都保持沉默,这是他们应该的。单击一个会适当地播放它,然后在播放时单击另一个会暂停第一个并播放新的(完美!)。唯一的问题是单击正在播放的任何一个都不会再暂停它。
  • 我猜这里,但尝试编辑我刚刚的答案。
【解决方案2】:

这个小功能对我有用。它会停止页面上播放的所有其他音频元素,并让已开始播放的元素继续播放。 Here 是个小提琴。

 $("audio").on("play", function (me) {
         jQuery('audio').each(function (i,e) {
              if (e != me.currentTarget)
              { 
                  this.pause(); 
              }
         });
 });

【讨论】:

  • 这在我的网站 nallikayi.com 上非常适合我。这可以在javascript中完成吗?
猜你喜欢
  • 2011-08-09
  • 1970-01-01
  • 2014-07-16
  • 2014-01-10
  • 2011-02-28
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多