【问题标题】:How can i make other audio/song be played when one of them stops javascript?当其中一个停止 javascript 时,如何播放其他音频/歌曲?
【发布时间】:2021-07-14 22:43:54
【问题描述】:

我是 html js 和 css 的新手,我的目标是做任何类似于 spotify 或任何播放音乐的应用程序,我的目标是当其中一个停止时播放其他音频。 我试过这个,但是当我加载页面时它不起作用,音频已经在“if”上声明了 10 秒并且弹出警报......这可能很容易,但是,你知道只是学习。

 if (audio.currentTime = 10) {
    
  alert("Hello! I am an alert box!!");
    
    }

【问题讨论】:

    标签: javascript html css web html5-audio


    【解决方案1】:

    你可以试试这个:

    var audio = document.getElementById('audio');
    var timed10 = false;
    audio.addEventListener('timeupdate', (time) => {
     if(time>=10 && !timed10){
      timed10 = true;
    
      // you can do any thing just here for example alert('hi')
    
      }
    });
    

    【讨论】:

      【解决方案2】:

      对于需要帮助的人,我这样做了:

          var audio = document.getElementById('audio');
      var play = document.getElementById('play');
      var count = 0;
      
      
          > function playaud(){   if(count == 0){
          >     count = 1;
          >     audio.play();
          >     audio2.load();
          >     audio3.load(); // stop other musics that could be playing before
          >     audio4.load();
          >     audio5.load();   }else{
          >     count = 0;
          >     audio.pause();
          >     audio.load(); // restart the music use audio.pause() to pause i instead of stop it
          >     count2 = 0;
          >     count3 = 0; //this to put the count at 0 becouse i have 5 musics and if they were playing before this one the count would stay at 1 and
          > that cause a bug 
          >     count4 = 0;
          >     count5 = 0;
          > 
          > 
          >   } }
      
      
      
      
      <button onclick="playaud()" id="play" type="button" name="button">Play &#9658;</button>
      

      【讨论】:

        【解决方案3】:

        您的 if 语句中有错误,您正在设置 audio.currentTime 的值而不是检查其值,在 JS 中您可以使用 == 来检查值是否匹配,与类型和 === 无关也会匹配类型,所以,如果你有:

        10 == "10" // 正确但是 10 === "10" // false 因为 "10" 是字符串而不是数字。

        我建议你看看MDN 了解 JS 中的 if 语句。

        【讨论】:

        • thx 寻求帮助,但如果我输入 if (audio.currentTime == 2) { alert("Hello!I am a alert box!!"); } 它只是没有做任何事情警报没有弹出
        • 呃,好吧,我从来没有使用过这个音频 api。感谢您的反馈意见。祝你好运。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 2013-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多