在上传多个文件之前获取音频/视频文件的总持续时间?

时间:2017-05-24 08:45:04

标签: javascript audio javascript-objects

  

如果我上传了多个音频文件,这将有助于我获取单个音频文件的持续时间我应该能够找到这些上传文件的总音频长度。

HTML CODE

<form action="#" method="post" enctype="multipart/form-data">
      File: <input type="file" name="fup" id="fup" /><br>
      Duration: <input type="text" name="f_du" id="f_du" size="5" /> seconds<br>
      <input type="submit" value="Upload" />
    </form>
    <audio id="audio"></audio>

    <script>
    // Code to get duration of audio /video file before upload - from: http://coursesweb.net/

    //register canplaythrough event to #audio element to can get duration
    var f_duration =0;  //store duration
    document.getElementById('audio').addEventListener('canplaythrough', function(e){
      //add duration in the input field #f_du
      f_duration = Math.round(e.currentTarget.duration);
      document.getElementById('f_du').value = f_duration;
      URL.revokeObjectURL(obUrl);
    });

    //when select a file, create an ObjectURL with the file and add it in the #audio element
    var obUrl;
    document.getElementById('fup').addEventListener('change', function(e){
      var file = e.currentTarget.files[0];
      //check file extension for audio/video type
      if(file.name.match(/\.(avi|mp3|mp4|mpeg|ogg)$/i)){
        obUrl = URL.createObjectURL(file);
        document.getElementById('audio').setAttribute('src', obUrl);
      }
    });
    </script>

0 个答案:

没有答案