2017-03-22 13 views
0

私はこのスレッドについて知っていますが、別の起動時にオーディオを一時停止するようにこの特定のスクリプトを変更しています。本当にありがとう。新しいサウンドが再生されたときに現在の音を一時停止するcss、html5 - audio、onclick

function playSound(el,soundfile) { 
    if (el.mp3) { 
     if(el.mp3.paused) el.mp3.play(); 
     else el.mp3.pause(); 
    } else { 
     el.mp3 = new Audio(soundfile); 
     el.mp3.play(); 
    } 
} 

答えて

0

最後に再生したサウンドオブジェクトを覚えておいて(次のサウンドを再生する前に一時停止する)必要があります。私はウェブ上で発見いくつかの音を使用し、簡単なスニペットを作っ:一時停止したときに、それが一時停止された最後の位置から再び再生した場合、あなたならば、サウンドは、再開されることを

var players = [{mp3: null}, {mp3: null}, {mp3: null}]; 
 

 
var currentAudio = null; 
 

 
function playSound(el,soundfile) { 
 
\t \t if(currentAudio){ 
 
    \t \t currentAudio.pause(); 
 
    } 
 

 
    if (el.mp3) { 
 
     if(el.mp3.paused){ 
 
     \t currentAudio = el.mp3; 
 
     \t el.mp3.play(); 
 
     } else { 
 
     \t el.mp3.pause(); 
 
     } 
 
    } else { 
 
     el.mp3 = new Audio(soundfile); 
 
     currentAudio = el.mp3; 
 
     el.mp3.play(); 
 
    } 
 
} 
 

 
document.getElementById('player1Button').addEventListener('click', function(){ 
 
    playSound(players[0], 'http://www.sousound.com/music/healing/healing_01.mp3'); 
 
}); 
 

 
document.getElementById('player2Button').addEventListener('click', function(){ 
 
    playSound(players[1], 'https://www.nasa.gov/mp3/590189main_ringtone_131_launchNats.mp3'); 
 
}); 
 

 
document.getElementById('player3Button').addEventListener('click', function(){ 
 
    playSound(players[2], 'https://www.nasa.gov/mp3/581097main_STS-1_Dust-it-Off.mp3'); 
 
}); 
 

 
document.getElementById('stop').addEventListener('click', function(){ 
 
    if(currentAudio){ 
 
    currentAudio.pause(); 
 
    } 
 
});
<button id="player1Button">Play 1</button> 
 
<button id="player2Button">Play 2</button> 
 
<button id="player3Button">Play 3</button> 
 
<button id="stop">Stop</button>

お知らせ停止して一時停止して巻き戻して、最初から再生を開始する場合は、一時停止後にAudioオブジェクトのcurrentTimeを0に設定する必要があります:currentAudio.currentTime = 0;

+0

ありがとうございます! – jordamagne

関連する問題