【问题标题】:onended audio event firing without playing the audioonended 音频事件触发而不播放音频
【发布时间】:2015-06-15 22:18:40
【问题描述】:

我有多个音频文件需要播放,只有在所有音频都播放完后才会显示下一部分。

我编写的代码会自动触发而不播放任何音频; 我该怎么做?这是我的代码:

<!DOCTYPE html> 
<html> 
<body> 

<p>Press play and wait for the audio to end.</p>

<audio id="myAudio" controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>


<audio id="myAudio2" controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>



<section id='asd'>
Hsfdads
</section>
<script>
var e= document.getElementById("asd");
console.log(e.style.display);
e.style.display='none';
</script>

<script>
var count=0;
function IncrementCount() {
    count+=1;
    console.log(count);
if (count ==2){
    var e= document.getElementById("asd");
    e.style.display='block';
    alert("The audios has ended");
count=0;
}
};
var aud = document.getElementById("myAudio");
aud.onended = IncrementCount() 


var aud2 = document.getElementById("myAudio2");
aud2.onended = IncrementCount()

</script>

</body> 
</html>

【问题讨论】:

    标签: javascript html dom-events audio-player


    【解决方案1】:

    我已经开始工作了。

    <!DOCTYPE html> 
    <html> 
    <body> 
    <script>
    var count=0;
    </script>
    <p>Press play and wait for the audio to end.</p>
    
    <audio id="myAudio" onended='IncrementCount()' controls>
      <source src="horse.ogg" type="audio/ogg">
      <source src="horse.mp3" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>
    
    
    <audio id="myAudio2" controls onended='IncrementCount()'>
      <source src="horse.ogg" type="audio/ogg">
      <source src="horse.mp3" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>
    
    
    
    <section id='asd'>
    Hsfdads
    </section>
    <script>
    var e= document.getElementById("asd");
    console.log(e.style.display);
    e.style.display='none';
    </script>
    
    <script>
    function IncrementCount() {
        count+=1;
        console.log(count);
    if (count ==2){
        var e= document.getElementById("asd");
        e.style.display='block';
        alert("The audios has ended");
        count=0;}
    };
    </script>
    
    </body> 
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      相关资源
      最近更新 更多