1

当您单击图像时,我正在使用此方法播放音频文件:

http://jsfiddle.net/v97Kq/3/

function imageSwitch(_imgID, _imgStart, _imgStop, _soundFileMp3, _soundFileOgg) {
    this.imgID = _imgID;
    this.imgStart = _imgStart;
    this.imgStop = _imgStop;
    this.song = new Audio();
    if (this.song.canPlayType("audio/mpeg"))
        this.song.src = _soundFileMp3;
    else 
        this.song.src = _soundFileOgg;
    this.song.loop = true;

    this.pos = 0;
    this.e;

    this.change = function () {
        if (this.pos == 0) {
            this.pos = 1;
            document.getElementById(this.imgID).src = this.imgStop;
            this.song.play();
        } else {
            this.pos = 0;
            document.getElementById(this.imgID).src = this.imgStart;
            this.song.pause();
        }
    }
}

效果很好!- 但是当点击另一个链接并开始另一个声音时,如何让它停止播放当前播放的声音?

4

1 回答 1

1

我发现这个解决方案可以很好地解决我上面的问题。我最终在这里找到了:

<script>
var currentPlayer;
function EvalSound(soundobj) {

 var thissound=document.getElementById(soundobj);
 if(currentPlayer  && currentPlayer != thissound) {
  currentPlayer.pause(); 
 }
 if (thissound.paused)
        thissound.play();
    else
    thissound.pause();
    thissound.currentTime = 0;
     currentPlayer = thissound;
}
</script>
于 2013-09-12T21:03:37.790 回答