87,994
社区成员




<!DOCTYPE html>
<html>
<head>
<!-- @author 夏茂轩@成都信息工程学院 QQ:1034297177 -->
<meta charset="utf-8"/>
<title></title>
<script language="JavaScript" src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(function () {
$("#test").change(function () {
var objUrl = getObjectURL(this.files[0]);
$("#audio").attr("src", objUrl);
$("#audio")[0].play();
$("#audio").show();
getTime();
});
});
<!--获取mp3文件的时间 兼容浏览器-->
function getTime() {
setTimeout(function () {
var duration = $("#audio")[0].duration;
if(isNaN(duration)){
getTime();
}
else{
console.info("该歌曲的总时间为:"+$("#audio")[0].duration+"秒")
}
}, 10);
}
<!--把文件转换成可读URL-->
function getObjectURL(file) {
var url = null;
if (window.createObjectURL != undefined) { // basic
url = window.createObjectURL(file);
} else if (window.URL != undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file);
}
return url;
}
</script>
</head>
<body>
<input id="test" type="file" multiple="multiple"/>
<audio id="audio" controls="" style="display: none;"></audio>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>html5预览mp3</title>
<script>
//jq不是做这种小事的
window.onload=function(){
var audio=document.getElementById("audio");
document.getElementById("test").onchange=function(e){
var file=e.target.files[0],src=window.createObjectURL&&window.createObjectURL(file)||window.URL&&window.URL.createObjectURL(file)||window.webkitURL && window.webkitURL.createObjectURL(file);
if(/^audio/i.test(file.type)){
audio.style.display='block';
audio.src=src;
audio.play();
function g(){isNaN(audio.duration) ? requestAnimationFrame(g):console.info("该歌曲的总时间为:"+audio.duration+"秒")}
requestAnimationFrame(g);
}else{
audio.pause();
audio.style.display='none';
alert("请选择音乐文件!");
}
}
}
</script>
</head>
<body>
<input id="test" type="file" multiple="multiple"/>
<audio id="audio" controls="" style="display: none;"></audio>
</body>
</html>