2013-08-19 43 views
0

我正在使用Soundmanager2播放mp3文件。我有一個play按鈕,它可以切換到pause。和一個單獨的stop按鈕。javascript/jquery停止按鈕將播放按鈕更改爲初始狀態不暫停

按'play',音樂播放,按鈕切換到'暫停'。按'pause'按鈕,音樂暫停。很棒。這是我的問題:

按'Play'音樂播放和按鈕更改爲'pause'按鈕。按Stop並停止音樂。這很棒,但播放按鈕仍然是一個'暫停'按鈕。按下它可將其彈回播放狀態,然後再次點擊播放音樂。

我需要以某種方式在下面的停止按鈕代碼下,按下停止按鈕時將播放按鈕重置爲初始狀態?

我嘗試在停止碼下添加'iteration=1',但這不起作用。

下面是javascript代碼:

$(function(){ 
$('.a5play').click(function(){ 
var iteration=$(this).data('iteration')||1 
switch (iteration) { 

case 1: 
soundManager.play('mySound1'); 
$('.a5play').text('Pause'); 
break; 

case 2: 
soundManager.pause('mySound1'); 
$('.a5play').text('Play'); 

break; 

} 
iteration++; 
if (iteration>2) iteration=1 
$(this).data('iteration',iteration) 
}) 
}) 

$('.a5stop').click(function(){ 
     interation=1; 
     soundManager.stop('mySound1'); 

        }); 

預先感謝任何幫助,您可以提供。

+0

試着設置一個[fiddle](http://jsfiddle.net),以便我們看到它是如何工作的? – adeneo

+0

我嘗試了一個小提琴,但沒有運氣,對不起...只是不想工作。我發現我可以通過在$ stop()函數中添加'$('。a5play')。text('Play');'來改變文本的播放方式。但音頻仍然只能再次點擊播放。我認爲'stop'函數需要將迭代變量設置回1,但我不知道該怎麼做? –

回答

0

我決定嘗試編寫一個替代方法來解決這個問題,並讓它按需要工作。因爲它可能對別人有幫助,所以我創建了這個:

var playbackState = 0; 

/*0 = play*/ 
/*1 = pause*/ 
    $('.a5play').click(function(){ 
     if(playbackState == "0") { 
playbackState="1"; 
soundManager.play('mySound1'); 
$('.a5play').text('Pause'); 
     } else { 
playbackState="0"; 
soundManager.pause('mySound1'); 
$('.a5play').text('Play'); 
     } 
    }); 
    $('.a5stop').click(function(){ 
     playbackState="0"; 
     soundManager.stop('mySound1'); 
     $('.a5play').text('Play'); 
    });