直接上代码,代码很简单,需要选择一个声音音乐文件
document.getElementById("file").addEventListener("change",function () {
var file = document.getElementById('file').files[0];
var url = URL.createObjectURL(file);
console.log(url);
document.getElementById("audio_id").src = url;
});
完整代码
<html>
<body>
<input type="file" id="file">
<audio id="audio_id" controls autoplay loop>Your browser can't support HTML5 Audio</audio>
<script>
document.getElementById("file").addEventListener("change",function () {
var file = document.getElementById('file').files[0];
var url = URL.createObjectURL(file);
console.log(url);
document.getElementById("audio_id").src = url;
});
</script>
</body>
</html>
以上是音频文件,如果是视频 ,将上面audio 改成 video 标签即可
<video id="audio_id" controls autoplay loop>Your browser can't support HTML5 Audio</video>