h5 audio播放音频文件
注:下面html中样式及不相关的内容去掉了
第一个例子
播放没有防盗链的外网音频文件是可以的
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>音乐</title>
<script src="Js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="Js/util.js" type="text/javascript"></script>
<link href="file/audio/css/public.css" rel="stylesheet" type="text/css">
<!--禁止屏幕缩放-->
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.2,user-scalable=no"/>
</head>
<body>
<div class="text_h2" style="margin-top: 10px; padding-left: 14px; padding-right: 14px;">
<span id="Title" style="letter-spacing: 2px; line-height: 25px; font-weight: 600;">标题<br></span>
</div>
<div class="text_h2" style="padding-left: 5px; padding-right: 5px;">
<div style="margin-top: 5px;"><img id="LogoUrl" src="">
</div>
</div>
<div style="background-color: #e2e2e2; margin-left: 15px; margin-right: 15px; border-radius: 2px;">
<div id="AudioTitle" style="font-weight: 600; text-align: center; padding-top: 12px;">标题</div>
<div style="text-align: center;" id="divAudio">
<audio id="SimpleMusicPlayer" controls="controls" style="background-color: #e2e2e2; width: 98%; margin-top: 12px; margin-bottom: 5px;">
</audio>
</div>
</div>
<div class="bottom_text" id="Contents" style="padding-left: 12px; padding-right: 12px; letter-spacing: 2px; line-height: 28px; font-weight: 600; margin-top: 10px; margin-bottom: 15px;">
内容区
</div>
<script type="text/javascript">
$(document).ready(function() {
GetData();
});
function GetData() {
var id = getQueryString("id") || 0;
if (id == 0) {
return;
}
var audio = document.getElementById("SimpleMusicPlayer");
var data = {};
data.method = "Get";
data.id = id;
ajaxProcess("/h5/audio.aspx?", data, function callSuccess(oRet) {
var result = oRet[0];
if (result != null) {
document.title = "我的音乐-" + result.Title;
$("#Title").html(result.Title + "<br>");
$("#AudioTitle").html(result.AudioTitle);
$("#Contents").html(result.Contents);
if (result.AudioUrls) {
audio.src = result.AudioUrls; //设置audio src的值
} else {
audio.src = "http://www.xxx.com/default.mp3";
}
audio.autobuffer = true;
audio.load();
audio.preload = "auto";
}
}, function callError(e) {
alert(e);
});
}
</script>
</body>
</html>
第二个例子
如果音频地址是别人网站的,且有防盗链,用例子一的方法则播放不了,用fiddler查看音频文件访问返回的是status 399,如果直接在浏览器打开这个地址则是可以播放的,初步怀疑是加了防盗链设置,解决如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>音乐</title>
<script src="Js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="Js/util.js" type="text/javascript"></script>
<link href="file/audio/css/public.css" rel="stylesheet" type="text/css">
<!--禁止屏幕缩放-->
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.2,user-scalable=no"/>
</head>
<body>
<div class="text_h2" style="margin-top: 10px; padding-left: 14px; padding-right: 14px;">
<span id="Title" style="letter-spacing: 2px; line-height: 25px; font-weight: 600;">标题<br></span>
</div>
<div class="text_h2" style="padding-left: 5px; padding-right: 5px;">
<div style="margin-top: 5px;"><img id="LogoUrl" src="">
</div>
</div>
<div style="background-color: #e2e2e2; margin-left: 15px; margin-right: 15px; border-radius: 2px;">
<div id="AudioTitle" style="font-weight: 600; text-align: center; padding-top: 12px;">标题</div>
<div style="text-align: center; background-color: #e2e2e2; width: 95%; margin-top: 12px; margin-bottom: 5px; height:50px;" id="divAudio">
<iframe id="audioPage" scrolling="no" frameborder="0" style="width:98%;height:45px; text-align: center;"> </iframe>
</div>
</div>
<!--<script type="text/javascript" src="file/audio/js/audio.js"></script>-->
<div class="bottom_text" id="Contents" style="padding-left: 12px; padding-right: 12px; letter-spacing: 2px; line-height: 28px; font-weight: 600; margin-top: 10px; margin-bottom: 15px;">
内容区
</div>
<script type="text/javascript">
$(document).ready(function() {
GetData();
});
function GetData() {
var id = getQueryString("id") || 0;
if (id == 0) {
return;
}
var data = {};
data.method = "Get";
data.id = id;
ajaxProcess("/h5/audio.aspx?", data, function callSuccess(oRet) {
var result = oRet[0];
if (result != null) {
document.title = "我的音乐-" + result.Title;
$("#Title").html(result.Title + "<br>");
$("#AudioTitle").html(result.AudioTitle);
$("#Contents").html(result.Contents);
var soundSrc = "http://www.xxx.com/default.mp3";
if (result.AudioUrls) {
soundSrc = result.AudioUrls;
}
var strSrc = "javascript:\"<!doctype html><html><head></head><body style='width:100%;height:45px; text-align: center;'><div><audio id='simplemusicplayer2' src='" + soundSrc + "' controls='controls' preload='metadata' autoplay style='width:95%;'></audio></div></body></html>\"";
$("#audioPage").attr("src", strSrc); //iframe的src设置的是这个page的html页面内容,这样就去掉了iframe页面内audio 元素src的来源 reffer
}
}, function callError(e) {
alert(e);
});
}
</script>
</body>
</html>
关键之处是让访问音频地址的时候reffer没有,其它的办法也可以,这里只是其中一种方法
--- end ---