【问题标题】:How to find duration of multiple audio files?如何查找多个音频文件的持续时间?
【发布时间】:2019-02-23 05:27:45
【问题描述】:

我正在使用以下代码上传多个文件。

<input accept=".mp3,.wav" type="file" id="files" class="file" multiple />

如何获取所有上传文件的时长?

【问题讨论】:

    标签: javascript jquery html vue.js


    【解决方案1】:

    [在head标签中包含moment.js库]:

    然后在更改函数中添加这个函数: $("#duration").text(file.duration);

    【讨论】:

      【解决方案2】:

      尝试关注

      var objectUrl;
      
      $("#audio").on("canplaythrough", function(e){
          var seconds = e.currentTarget.duration;
          var duration = moment.duration(seconds, "seconds");
          
          var time = "";
          var hours = duration.hours();
          if (hours > 0) { time = hours + ":" ; }
          
          time = time + duration.minutes() + ":" + duration.seconds();
          $("#duration").text(time);
          
          URL.revokeObjectURL(objectUrl);
      });
      
      $("#file").change(function(e){
          var file = e.currentTarget.files[0];
         
          $("#filename").text(file.name);
          $("#filetype").text(file.type);
          $("#filesize").text(file.size);
          
          objectUrl = URL.createObjectURL(file);
          $("#audio").prop("src", objectUrl);
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <p>Select a .mp3 file</p>
      <input type="file" id="file" accept=".mp3,.wav" />
      
      <audio id="audio"></audio>
      
      <p>
        <label>File Name:</label>
        <span id="filename"></span>
      </p>
      
      <p>
        <label>File Type:</label>
        <span id="filetype"></span>
      </p>
      
      <p>
        <label>File Size:</label>
        <span id="filesize"></span>
      </p>
      
      <p>
        <label>Song Duration:</label>
        <span id="duration"></span>
      </p>

      【讨论】:

      • 需要上传多个文件并获取所有文件的时长
      【解决方案3】:

      要获取音频文件的长度,请使用 audio 持续时间属性:

      var duration = document.getElementById("files").duration;
      

      【讨论】:

      • 未定义
      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2013-10-27
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多