播放音频文件时用键盘播放时向前和向后搜索十秒

Playing audio file to seek by ten seconds forward and backward while playing with keyboard

我可以通过键盘播放暂停和停止音频文件,但是当我来回搜索时,我使用左右方向键,并且正在搜索15秒。相反,我需要做 5 或 10 秒。下面是我使用的java脚本

       <script>
    var audio = $("audio")[0];
    $(document).keydown(function (e) {
        var unicode = e.charCode ? e.charCode : e.keyCode;
        console.log(unicode);
        // right arrow
        if (unicode == 39) {
            audio.currentTime += 5;
            // back arrow
        } else if (unicode == 37) {
            audio.currentTime -= 5;
            // spacebar
        } else if (unicode == 32) {
            if (audio.paused) {
                audio.play();
            }
            else {
                audio.pause()
            }
        }
    });
</script>

I am seeking back and forth, I am using left and right arrow keys, and it is being seeking 15 sec. I need to do it for 5 or 10

您可以使用 <audio> 元素的 HTMLMediaElement.currentTime 来设置音频开始播放的位置; +=-= 运算符将 audio.currentTime 递增或递减 n 秒,例如 5 或 10seconds; check [HTMLMediaElement.paused][2] to toggle callingaudio.play()oraudio.pause()`

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<audio controls autoplay src="https://upload.wikimedia.org/wikipedia/commons/6/6e/Micronesia_National_Anthem.ogg"></audio>
<script>
  var audio = $("audio")[0];
  $(document).keydown(function(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    console.log(unicode);
      // right arrow
    if (unicode == 39) {
      audio.currentTime += 5;
      // back arrow
    } else if (unicode == 37) {
      audio.currentTime -= 5;
      // spacebar
    } else if (unicode == 32) {
      if (audio.paused) {
        audio.play();
      } 
      else {
        audio.pause()
      }
    }
  });
</script>