简体   繁体   English

使用javascript使用向上和向下箭头控制音量

[英]controlling the volume using up and down arrows using javascript

Using the below javascript i am able to play and pause the audio using space bar and move forward and backward by using left and right arrows but now i need to increase and decrease the volume using up and down arrows and when i click on home it should go to starting of the audio file and when i click on end button it should go to end of the audio file.how can i do this 使用下面的javascript,我可以使用空格键播放和暂停音频,并使用左右箭头向前和向后移动,但现在我需要使用向上和向下箭头增加和减少音量,当我点击主页它应该去开始音频文件,当我点击结束按钮时,它应该转到音频文件的结尾。我可以这样做

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

For the volume, you should try this: 对于音量,你应该试试这个:

var audio = $("audio")[0];
var source = audioCtx.createMediaElementSource(audio);
var gainNode = audioCtx.createGain();

$(document).keydown(function(e) {
  var keycode = e.which;

  if (keycode == 39) { // right arrow
    audio.currentTime += 5;
  } else if (keycode == 37) { // left arrow
    audio.currentTime -= 5;
  } else if (keycode == 38) { // up arrow
    gainNode.gain.value += 0.1;
  } else if (keycode == 40) { // down arrow
    gainNode.gain.value -= 0.1;
  } else if (keycode == 32) { // spacebar
    if (audio.paused) {
      audio.play();
    } else {
      audio.pause()
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM