HTML audio can't set currentTime

后端 未结 8 839
予麋鹿
予麋鹿 2020-11-30 05:22

I am using Chrome. In my dev tools console, I tried the following:

Everything works as expected except last line. Why can\'t I set currentTime

相关标签:
8条回答
  • 2020-11-30 06:07

    I ran into this problem after I'd started to use the PHP Server extension to VS Code to serve my HTML/PHP local testing. The problem was resolved for me by going back to my old Abysswebserver setup.

    So, it's not simply that "you can't manipulate .currentTime on locally served files" but rather that "you need to pick a server that gives you the right headers". The Status entry for my file from AbyssWebserver, for example, read "Status Code: 206 Partial Content" while the one from PHP Server read "Status Code: 200 OK".

    There are other differences though so there may be more to this than just the Status entry. See https://github.com/brapifra/vscode-phpserver/issues/85, for a full header comparison.

    0 讨论(0)
  • 2020-11-30 06:08

    My guess is that '10' is longer that you mp3's length.

    But that logs the length of mp3 instead of '0'.

    0 讨论(0)
  • 2020-11-30 06:18

    You need to do something like this (if you use jQuery)

    $('#elem_audio').bind('canplay', function() {
      this.currentTime = 10;
    });
    

    or in Javascript

    var aud = document.getElementById("elem_audio");
    aud.oncanplay = function() {
        aud.currentTime = 10;
    };
    

    The reason behind for this setup is you need to make sure the audio is ready to play.

    0 讨论(0)
  • 2020-11-30 06:18

    The solution which worked for me was not setting "src" on directly, but use with type attribute, maybe type attribute is helping browser some way.

    0 讨论(0)
  • 2020-11-30 06:19

    The only solution for setting currentTime I got to work reliably was using the onprogress event.

    audio.onprogress = function() {
      if (audio.currentTime == 0) {
        audio.currentTime = 10;
      }
    }
    
    0 讨论(0)
  • 2020-11-30 06:20

    Check your HTTP server configuration, in my testing environment ( Chrome 69 on Mac OS) setting currentTime property of audio element works only when the audio source is served by a HTTP server support persistent connection.

    If the HTTP server you used support persistent connection, you will found (in Chrome DevTool) the Connection field of response headers of your audio source be keep-alive. By contrast if the audio source is served by a persistent connection incompatible server, there will be no Connection field in response headers.

    The status code of your audio source HTTP request will be a reference too, 206 Partial Content for persistent connection supported server, 200 OK for an inferior one.

    0 讨论(0)
提交回复
热议问题