【发布时间】:2019-02-23 05:27:45
【问题描述】:
我正在使用以下代码上传多个文件。
<input accept=".mp3,.wav" type="file" id="files" class="file" multiple />
如何获取所有上传文件的时长?
【问题讨论】:
标签: javascript jquery html vue.js
我正在使用以下代码上传多个文件。
<input accept=".mp3,.wav" type="file" id="files" class="file" multiple />
如何获取所有上传文件的时长?
【问题讨论】:
标签: javascript jquery html vue.js
[在head标签中包含moment.js库]:
然后在更改函数中添加这个函数: $("#duration").text(file.duration);
【讨论】:
尝试关注
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>
【讨论】:
要获取音频文件的长度,请使用 audio 持续时间属性:
var duration = document.getElementById("files").duration;
【讨论】: