0

How to get athe duration of an audio file?

function handleFileSelect(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    var files = evt.dataTransfer.files,

        dur = files.duration;

    document.getElementById('drop_zone-info').innerHTML = 'Duration: ' + dur + ' (h:m:s)';
}

Test: http://jsfiddle.net/XSavU/1/

Martin
  • 2,007
  • 6
  • 28
  • 44
  • 1
    You'll have to read the file to find out that information. – Musa Jun 09 '13 at 15:20
  • By adding a fake audio tag for and get `audio.duration`? – Martin Jun 09 '13 at 15:31
  • I was thinking using [`FileReader`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) but your idea sounds much easier, try it an see if it works. – Musa Jun 09 '13 at 15:34
  • Can't get the proper src for the audio tag since only `file.name` is possible without the path. Faking the path gives me "Not allowed to load local resource" – Martin Jun 09 '13 at 15:36
  • 1
    http://jsfiddle.net/XSavU/2/ – Musa Jun 09 '13 at 15:48
  • Got it: http://jsfiddle.net/XSavU/3/ Thanks! – Martin Jun 09 '13 at 16:17
  • Now I got the next problem. I can't adress them on multiple files: http://jsfiddle.net/H7xFS/ – Martin Jun 09 '13 at 19:53
  • You're trying to get the element from the dom before you add it to the dom – Musa Jun 09 '13 at 19:55
  • It seems I can't join the dur and the size into one statement :( http://jsfiddle.net/H7xFS/1/ – Martin Jun 09 '13 at 20:08
  • Take a look at http://stackoverflow.com/questions/15482516/for-loops-with-mutable-variables/15482653#15482653 , http://api.jquery.com/on/ and then http://jsfiddle.net/H7xFS/2/ – Musa Jun 09 '13 at 20:13
  • I see, on() runs an own function so the index needs to ne stored as `e.data`. That was a had sunday lesson! Thanks a lot! – Martin Jun 09 '13 at 20:40

0 Answers0