javascript / jquery停止按钮将播放按钮更改为初始状态而不是暂停

时间:2013-08-19 11:27:56

标签: javascript jquery toggle playback soundmanager2

我正在使用 Soundmanager2 来播放mp3文件。我有一个play按钮,可以切换pause。和一个单独的stop按钮。

按“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');

                    });

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分: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');
    });