简体   繁体   中英

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

 <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()
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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