You must Sign In to post a response.
  • Category: JavaScript

    Unable to fast forward and slow the audio playing using javascript

    When i run my aspx page in the web page i will get an audio which can be controlled using keyboard keys F10 key is used to play and pause the audio,F11 is used forward the audio by 5 sec,F9 is used to backward the audi by 5 sec,F7 is used to fast forward the audio and F8 is used to slow the audio that means when i use F7 and F8 the audio should increase its speed than the normal speed and when i use F8 the audio should become slow than the original speed.Below the audio i have ck editor where i should be able to control the audio with all the above mentioned functionalities.

    Everything works that is F10,F11 and F9 are working but F7 and F8 are not working that is fast forwarding and backwarding are not working how can i do this.

    <script type = "text/javascript">
    var audio = document.getElementById("player");

    window.addEventListener("keydown", playPauseKb, false);

    function playPauseKb(event) {
    var x = event.keyCode;
    console.log(x);

    if (x == 120) {
    audio.currentTime -= 5;//F9 Rewined
    }

    else if (x == 122) {
    //event.data.$.preventDefault();
    audio.currentTime += 5;//F11 forword
    }

    else if (x == 121) {
    if (audio.paused) {
    audio.play();//F10 paly/pause
    }
    else {
    audio.pause()
    }
    }
    else if (x == 118)
    {
    Slow();
    event.data.$.preventDefault();

    }
    else if (x == 119)
    {
    Forward();
    event.data.$.preventDefault();

    }
    }
    var count = 0;
    function Forward() {
    event.data.$.preventDefault();
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.4;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    event.data.$.preventDefault();
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    window.addEventListener("keydown", function (e) {
    // f6, page up, page down and arrow keys:
    if ([117, 119, 120, 121, 122, 123, 113, 33, 34, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
    e.preventDefault();
    }
    }, false);
    </script>

    <script type = "text/javascript">
    $(document).ready(function () {
    CKEDITOR.on('instanceCreated', function (e) {
    e.editor.on('contentDom', function () {
    e.editor.document.on('keydown', function (event) {

    if (event.data.$.keyCode == 120) {
    event.data.$.preventDefault();
    audio.currentTime -= 5;//f10_Rewind
    }

    else if (event.data.$.keyCode == 122) {
    event.data.$.preventDefault();
    audio.currentTime += 5;//F11 farward
    }
    else if (event.data.$.keyCode == 121) {
    event.data.$.preventDefault();
    if (audio.paused) {
    audio.play();//f6_play and pause
    }
    else {
    audio.pause()
    }
    }
    else if (event.data.$.keyCode == 118) {
    event.data.$.preventDefault();
    Slow();
    }
    else if (event.data.$.keyCode == 119) {
    event.data.$.preventDefault();
    Forward();
    }
    });
    var count = 0;
    function Forward() {
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }

    });
    });
    });

    </script>
  • #767744
    Hai Kavitha,
    it is very difficult to answer the question by reproducing your scenario. Instead, you need to explain where is the exact problem in your code so that we can suggest correctly.
    As you said "F7 and F8 are not working that is fast forwarding and back-warding are not working ". it means you need to debug this code and when you press F7 and F8, what exactly happens? Did you check? Are you getting any error message or something else. Is the event is not firing? not attaching with event listener?
    Put a debugger to your code and then press these keys and check what is exactly happening there for these keys in the code. There could be something which is not handled properly.
    The below code is for F7 and F8

    else if (x == 118)
    {
    Slow();
    event.data.$.preventDefault();
    }
    else if (x == 119)
    {
    Forward();
    event.data.$.preventDefault();
    }

    Try flipping the code as below:

    else if (x == 118)
    {
    event.data.$.preventDefault();
    Slow();
    }
    else if (x == 119)
    {
    event.data.$.preventDefault();
    Forward();
    }

    Also provided only that part where it is failing. As the code looks OK so you need to find out when it is in debug mode and check for the issue. Once you get the issue, we can suggest something to fix it.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767748
    i had tried in both the ways sir even by flipping my code but it is not working sir problem is with F7 and F8 keys sir to check it in debugging mode the audio does not works in internet explorer and to check in chrome i cannot cannot debugger could any one please help me out

  • #767766
    Are you using HTML5 features, it has good audio player with following features
    1. Play/Pause
    2. Rewind
    3. Fast-forward
    4. Previous
    5. Next
    6. Stop
    7. Restart
    8. Volume up
    9. Volume down.
    just use AUDIO tag for it, see below link

    < audio id="myaudio" controls="controls" preload="auto">
    HTML5 audio not supported
    < /audio>

    see below javascript player
    http://imajineweb.com/javascriptaudioplayer

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767771
    but i need them in keydown events the problem was solved but i need to control the audio with these same keyboard keys from new tab that is opened in the browser how can i do this


Sign In to post your comments