1

请看这个小提琴http://jsfiddle.net/rabelais/yLdkj/1/

上面的小提琴显示了悬停播放音频的三个小节。如何更改此设置,以便音乐播放并在点击时暂停。此外,如果一个音频正在播放而另一个被点击,那么已经播放的歌曲如何暂停?

 $("#one").mouseenter(function () {
 $('#sound-1').get(0).play();
 });
$("#one").mouseleave(function () {
$('#sound-1').get(0).pause();
});
$("#two").mouseenter(function () {
$('#sound-2').get(0).play();
});
$("#two").mouseleave(function () {
$('#sound-2').get(0).pause();
});
$("#three").mouseenter(function () {
$('#sound-3').get(0).play();
});
$("#three").mouseleave(function () {
$('#sound-3').get(0).pause();
});
4

3 回答 3

1

尝试

<div id="sound-bars">
    <div id="one" class="bars" data-target="#sound-1"></div>
    <div id="two" class="bars" data-target="#sound-2"></div>
    <div id="three" class="bars" data-target="#sound-3"></div>
</div>
<audio id="sound-1" class="sound">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-2" class="sound">
    <source src="http://angelaandtom.co.uk/lib/audio/you did not listen .mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/you did not listen .oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-3" class="sound">
    <source src="http://angelaandtom.co.uk/lib/audio/can you hear me .mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/can you hear me .oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>

然后

var $sounds = $('.sound'),
    $bars = $('#sound-bars .bars');
$bars.click(function () {
    var $this = $(this),
        $target = $($this.data('target')),
        target = $target[0];
    $bars.not(this).removeClass('playing');
    $sounds.not($target).each(function () {
        this.pause();
    });

    $this.toggleClass('playing');
    if ($this.hasClass('playing')) {
        target.play();
    } else {
        target.pause();
    }
})

演示:小提琴

于 2013-11-08T09:38:06.047 回答
0

您可以在单击 div 时检查暂停所有音频,然后播放相关音频。您也可以稍微整理一下代码:

jQuery:

$(".playbtn").click(function() {
    // Pause all audio
    $("audio").each(function(index, elem) {
        elem.pause();
    });
    // Grab the associated sound ID
    var soundId = $(this).data("soundid");
    // Play the audio
    $("#sound-"+soundId)[0].play();
});

HTML:

<div class="playbtn" data-soundid="1">Play #1</div>
<div class="playbtn" data-soundid="2">Play #2</div>
<div class="playbtn" data-soundid="3">Play #3</div>
<div class="playbtn" data-soundid="4">Play #4</div>

<audio id="sound-1">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-2">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-3">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-4">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
于 2013-11-08T09:28:15.603 回答
0
$("#one").click(function () {
  pauseAll();
    $('#sound-1').get(0).play();
});

$("#two").click(function () {
pauseAll();         
    $('#sound-2').get(0).play();
});

$("#three").click(function () {
      pauseAll();
    $('#sound-3').get(0).play();
});
function pauseAll(){
  $("audio").each(function (){
        this.pause();
    });
}

这在这里工作正常。

于 2013-11-08T09:29:23.197 回答