1

I am able to play pause and stop audio files through the keyboard, but when 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 seconds, instead. Below is the java script that i used

       <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>
  • _"and it is being seeking 15 sec. I need to do it for 5 or 10 seconds, instead."_ You could substitute `5` or `10` for `15`? Can you include `js` where "seking 15 sec." is set at Question? See http://stackoverflow.com/help/mcve – guest271314 May 26 '16 at 04:40
  • It is coming by default i had not set 15 seconds anywhere in my code can you please tell me how can i do this –  May 26 '16 at 04:45
  • thanks a lot your code helped me a lot when i am testing it individually but when i am integrating it to my code its not working could you please provide this same working functionality to my javascript code that i had posted –  May 26 '16 at 06:55
  • I'm voting to close this question as off-topic because this exact question was asked 2 days prior by a user with the same name (abcd): http://stackoverflow.com/questions/37417808/my-jquery-is-not-seeking-the-right-amount-of-time-on-my-audio-files – Michael Gaskill Jun 10 '16 at 02:38

2 Answers2

7

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

You can use HTMLMediaElement.currentTime of <audio> element to set where audio should begin playing; += or -= operators to increment or decrement audio.currentTime by n seconds, for example 5 or 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>
guest271314
  • 1
  • 15
  • 104
  • 177
  • where should i use this audio.currentTime = 5; audio.play(); in my js code can you please tell me also i am attaching my js now –  May 26 '16 at 04:54
  • @abcd See http://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime/ – guest271314 May 26 '16 at 04:57
  • but in my javascript i am not using get element by id anywhere can you please modify my javascript code by implementing your code –  May 26 '16 at 05:03
  • @abcd Can you include `html` at Question? – guest271314 May 26 '16 at 05:09
  • i had included htm5 audio in my question as it is html5 audio file –  May 26 '16 at 05:14
  • @abcd Can you include the `html` that you are using at Question? , create stacksnippets to demonstrate? See http://stackoverflow.com/help/how-to-ask – guest271314 May 26 '16 at 05:15
  • i had added my code that is in my aspx can you please refer it once –  May 26 '16 at 05:19
  • @abcd Have not tried `aspx`. Can you include both `html` and `js` at Question? Have you read link at http://stackoverflow.com/questions/37451606/playing-audio-file-to-seek-by-ten-seconds-forward-and-backward-while-playing-wit/37451848?noredirect=1#comment62404536_37451606 ? – guest271314 May 26 '16 at 05:21
  • I had included both html and javascript in my question can you please help me out –  May 26 '16 at 05:26
  • thanks a lot your code helped me a lot when i am testing it individually but when i am integrating it to my code its not working could you please provide this same working functionality to my javascript code that i had posted –  May 26 '16 at 06:56
  • @abcd _"could you please provide this same working functionality to my javascript code that i had posted"_ `js` at post uses same pattern at, addresses original Question _"Playing audio file to seek by ten seconds forward and backward while playing with keyboard"_ – guest271314 May 26 '16 at 07:09
  • but without using the script also happens the same,that is just by using the audio control only when i am using left and right arrows it forwards by 15sec and by using ctrl+left and ctrl+right arrows it goes by 5 sec by using javascript or without using javascript –  May 26 '16 at 07:18
0

This works, if the audio file is "short enough". But if the audio file is like one hour (e.g. an audio book), this solution fails. Sampling audio.currentTime don't work "fast enough" in large audio files, i.e. the audio may jump before currentTime is sampled. I solved this by taking samples continuously before Inc/Dec -keys, and setting an absolute value in key press.

var IncDecValue = 20;
  var audio = $('audio')[0];
  var taketime;                     // Take sample just before arrow-key
  setInterval(function () {taketime = audio.currentTime}, 300);
  
      $(document).keydown(function(e) {
          var unicode = e.charCode ? e.charCode : e.keyCode;
          console.log(unicode);
          if (unicode == 39) {      // right arrow
            audio.currentTime = taketime + IncDecValue;  
          } else if (unicode == 37) { // back arrow
            audio.currentTime = taketime - IncDecValue;
          } else if (unicode == 32) { // spacebar
            if (audio.paused) {
              audio.play();
            } 
            else {
              audio.pause()
            }
      }
  });
Twr
  • 1