2016-06-15 179 views
0

我想做4播放按鈕,播放MP3音頻。播放功能每4個按鈕工作一次,但不幸的是,暫停對其中任何一個都不起作用。也許暫停情況是錯誤的?jQuery:多個音頻播放器按鈕

<div class="audio-button"> 
    <audio src="media/test.mp3"></audio> 
</div> 
<div class="audio-button"> 
    <audio src="media/test2.mp3"></audio> 
</div> 
<div class="audio-button"> 
    <audio src="media/test3.mp3"></audio> 
</div> 
<div class="audio-button"> 
    <audio src="media/test4.mp3"></audio> 
</div> 
$(document).ready(function() { 
    var playing = false; 

    $('.audio-button').click(function() { 
     $(this).toggleClass("playing"); 
     var song = $(this).find('audio').attr('src'); 
     var audio = new Audio(song); 

     if (playing == false) { 
      audio.play(); 
      playing = true; 
     } else { 
      audio.pause(); 
      playing = false; 
     } 
    }); 
}); 
+0

爲什麼你 「克隆」 的聲音(由'VAR音頻=新的音頻(曲);')?試試'$(this).find('audio')[0] .pause()' –

回答

1

您需要直接從按鈕抓取音頻元素作爲子元素並與其進行交互。您不需要使用構造函數來克隆Audio對象。

$(document).ready(function() { 
 
    var playing = false; 
 

 
    // Add file names. 
 
    $('.audio-button').each(function() { 
 
    var $button = $(this);  
 
    var $audio = $button.find('audio'); 
 
    
 
    $($('<span>').text($audio.attr('src'))).insertBefore($audio); 
 
    }); 
 

 
    // Add click listener. 
 
    $('.audio-button').click(function() { 
 
    var $button = $(this);  
 
    var audio = $button.find('audio')[0]; // <-- Interact with this! 
 
    
 
    // Toggle play/pause 
 
    if (playing !== true) { 
 
     audio.play(); 
 
    } else { 
 
     audio.pause(); 
 
    } 
 

 
    // Flip state 
 
    $button.toggleClass('playing'); 
 
    playing = !playing 
 
    }); 
 
});
.fa.audio-button { 
 
    position:  relative; 
 
    display:  block; 
 
    width:   12em; 
 
    height:  2.25em; 
 
    margin-bottom: 0.125em; 
 
    text-align: left; 
 
} 
 

 
.fa.audio-button:after { 
 
    position:  absolute; 
 
    right:   0.8em; 
 
    content:  "\f04b"; 
 
} 
 

 
.fa.audio-button.playing:after { 
 
    content:  "\f04c"; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button class="fa audio-button"> 
 
    <audio src="media/test.mp3"></audio> 
 
</button> 
 
<button class="fa audio-button"> 
 
    <audio src="media/test2.mp3"></audio> 
 
</button> 
 
<button class="fa audio-button"> 
 
    <audio src="media/test3.mp3"></audio> 
 
</button> 
 
<button class="fa audio-button"> 
 
    <audio src="media/test4.mp3"></audio> 
 
</button>

+0

謝謝!它現在有效。 –

0

你想被點擊時一個停止其他三個?

如果這樣你就可以嘗試:

$(document).ready(function() { $('.audio-button').click(function (e) { var button = $(e.target).closest('.audio-button'); var audio = button.find('audio'); var audioDom = audio.get(0); $('audio[data-is-playing]').not(audio).each(function (i, currentAudioDom) { var currentButton = $(currentAudioDom).closest('.audio-button'); currentButton.removeClass('playing'); currentButton.removeAttr('data-is-playing'); currentAudioDom.$audio.pause(); }); button.addClass('playing'); audio.attr('data-is-playing', ''); audioDom.$audio = audioDom.$audio || new Audio(audio.attr('src')); audioDom.$audio.play(); }); });

修復:

其實我不知道,如果音頻元素本身就是一個音頻對象,如果是這樣,你可以調用播放/暫停直接在audioDom/currentAudioDom元素上,忘記$ audio的東西...