0

ok so i did some research and it seemed the best way to determine the length of an audio file with javascript is to create an <audio> tag, set the file path to the src attribute, append the child to the DOM, than bind a loadedmetadata eventListener on the <audio> tag and use the duration property of the audio tag to get the length of the audio file. This works great except...

sometimes, and randomly, the loading of the audio file will fail. my code makes the request for the file but it just never receives a response. The tag is still created and appended to the DOM but tag.duration = NaN.

Is there any other way to determine the length of an audio file with javascript? this is all part of a hack to a Qualtrics survey which is incredibly restrictive so i would like to stick to regular javascript (no jquery) because it is a serious hassle and buggy to try to use jquery with qualtrics.

to make things more annoying, the file that i request is actually a php file with the file name as a query parameter, ex File.php?F=92kls02jf9. this is how qualtrics does it so don't ask...not sure if this helps or hurts but i figured id add it in.

655321
  • 411
  • 4
  • 26
celeriko
  • 1,564
  • 1
  • 13
  • 26

1 Answers1

0

You may try an AJAX request sending an HTTP HEAD. A reference:

Content-Length header with HEAD requests?

Community
  • 1
  • 1
pid
  • 11,472
  • 6
  • 34
  • 63
  • ok, but its the browser that requests the audio file when i append the ` – celeriko Jan 26 '14 at 18:15
  • In JS you don't really need to attach an ` – pid Jan 26 '14 at 18:32
  • ok but `new Audio()` does the same thing as attaching an ` – celeriko Jan 26 '14 at 18:45
  • Wrong. When loading *manually* you have events similar to AJAX, like onerror: http://www.htmlgoodies.com/html5/tutorials/HTML5-Development-Class-Media-Events-3883356.htm#fbid=fRR11bM14Ye – pid Jan 26 '14 at 19:26
  • ok but that still doesn't help me get the duration of the audio file when the load fails. – celeriko Jan 26 '14 at 19:28
  • Well... no, it doesn't help you much. It can give you a fallback of some sort, act on it retrying to restart and similar. But yes, in the end it's not a quick solution to your problem. I can't help you much more than this. You should rephrase the question, add some code and post it again, hoping that you can get someone else to answer you. Still, have a nice day! – pid Jan 26 '14 at 19:33
  • ok yeah i already have a fallback for when it errors where it just takes a guess at how long the audio file will be. its just a lame solution. thanks for the help anyways, i don't think there is a way to do what i want within the limitations of this sh**ty Qualtrics software...oh well – celeriko Jan 26 '14 at 19:39
  • In my experience such an issue is often bound to the server, not the client. With a *perfect* server (no lag, no nothing slowing it down) very simple JS actions (like new Audio() or adding an audio node) work very well. As soon as it is a *real* server with traffic and all, that stuff happens inevitably, now and then. – pid Jan 26 '14 at 19:46
  • yeah...and of course i have no access to the server, thanks again buddy! – celeriko Jan 26 '14 at 22:06