1 回答
TA贡献1834条经验 获得超8个赞
您在示例中没有提供太多可使用的内容,但我创建了一个可能有帮助的代码片段。
运行下面的代码片段:
//declare elements as vars
var audioUpload = document.getElementById("file");
var audioLink = document.getElementById('audioLink');
var audioSource = document.getElementById("audioSource");
var audioControl = document.getElementById('audio');
//attach event listener to audioUpload input
audioUpload.addEventListener("change", function() {
uploadAudio(this);
});
//function that will upload audio
function uploadAudio(input) {
var reader;
if (input.files && input.files[0]) {
reader = new FileReader();
reader.onload = function(e) {
audioLink.setAttribute('data-value', e.target.result);
audioLink.innerHTML = "Click to play"
}
reader.readAsDataURL(input.files[0]);
}
}
//function that will play audio after clicking link
function play(element) {
audioSource.src = element.getAttribute('data-value');
audioControl.load();
audioControl.play();
};
<p>Upload your audio file:</p>
<input type="file" id="file">
<br/><br/>
<div>
<a id="audioLink" href="#" onclick="play(this)" data-value=""></a>
</div>
<br/><br/>
<audio controls="controls" id="audio">
<source id="audioSource" src=""></source>
</audio>
添加回答
举报