2013-09-08 1 views
1

J'utilise cette méthode pour lire des fichiers audio lorsque vous cliquez sur une image:HTML5 Audio - arrêter la lecture en cours quand un autre son démarrage

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(); 
     } 
    } 
} 

Il fonctionne bien! - mais comment puis-je l'empêcher d'écouter le son en cours de lecture lorsqu'un autre lien est cliqué et qu'un autre son commence?

Répondre

0

J'ai trouvé cette solution à mon problème ci-dessus, fonctionne parfaitement. que j'ai finalement trouvé here:

<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> 
Questions connexes