微信的audio无法自动播放的问题
一、问题
最近做了一个html5的项目,里面涉及到音乐播放,项目要求音乐进入页面就自动播放,于是我就想到了html5的audio标签,将mp3引入进去。
1.在audio标签里引入了autoplay属性;
经过测试发现Android上可以自动播放,ios上无法自动播放。
<audio id="audio" src="1.mp3" autoplay="autoplay"></audio>
2.在js文件中执行audio.play();
经过测试发现Android上可以自动播放,ios上无法自动播放。
var audio=document.querySelector("#audio");
audio.play();
二、原因
ios 为了节省用户流量,对于 audio 和 video标签的 preload 和 autopaly 标签 会自动拦截,
除非用户手动点击 交互才会执行 。
三、解决办法
前提:进入页面就自动播放
1.方法一
ps:此方法转载自(http://www.cnblogs.com/xiezhonglong/p/5780942.html)
//使用微信现在提供过的微信js-sdk 在ready中进行播放便可。 //首先引用js :
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> //然后写方法 : function autoPlayAudio1(){
wx.config({
// 配置信息, 即使不正确也能使用 wx.ready
debug: false,
appId: '',
timestamp: 1,
nonceStr: '',
signature: '',
jsApiList: []
});
wx.ready(function() {
document.getElementById('audio').play();
});
}
autoPlayAudio1();
2、方法二
var audio=document.querySelector("#audio");
document.addEventListener("WeixinJSBridgeReady",function(){
audio.play();
},false);
前提:异步操作的自动播放(比如页面2s之后自动播放音乐)
要实现异步操作下的自动播放,上边的第一种方法可以用,第二种方法已经不适用
1.方法一
//使用微信现在提供过的微信js-sdk 在ready中进行播放便可。 //首先引用js :
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> //然后写方法 : function autoPlayAudio1(){
wx.config({
// 配置信息, 即使不正确也能使用 wx.ready
debug: false,
appId: '',
timestamp: 1,
nonceStr: '',
signature: '',
jsApiList: []
});
wx.ready(function() {
document.getElementById('audio').play();
});
} setTimeout(autoPlayAudio1,2000);
2.方法二
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title> </head> <body>
<div id="div1" style="display: none;"></div>
<audio src="1.mp3" id="audio"></audio>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
setTimeout(function() {
wx.getNetworkType({
success: function(res) {
document.querySelector("#audio").play();
}
});
}, 2000);
</script>
</html>
3.方法三
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="div1" style="display: none;"></div>
<audio src="1.mp3" id="audio"></audio>
</body>
<script type="text/javascript">
setTimeout(function(){
//下面的'getNetworkType'并不是固定不变的,可以用'getLocation'等替代
WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
document.querySelector("#audio").play();
});
},2000);
</script>
</html>
4.方法四
该方法是用的HTML5的音频接口AudioContext来实现的,查看更多
PS:该方法暂停之后重新播放是从头开始,与audio标签不同
function MyAudio(url) {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
this.audioContext = new AudioContext();
this.audioBuffer = null;
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "arraybuffer";
var _this = this;
xhr.onload = function() {
_this.audioContext.decodeAudioData(xhr.response, function(buffer) {
_this.audioBuffer = buffer;
console.log(this.currentTime);
if(typeof callback == "function") {
callback.call(_this, buffer);
}
});
}
xhr.send();
}
//执行音乐播放
MyAudio.prototype.play = function() {
if(this.audioBuffer) {
this.source = this.audioContext.createBufferSource();
this.source.buffer = this.audioBuffer;
this.source.connect(this.audioContext.destination);
this.source.start(0); } }
//执行音乐停止
MyAudio.prototype.stop = function(buffer) {
if(this.source) {
this.source.stop(0);
}
} var audio = new MyAudio("./1.mp3");
setTimeout(function() {
audio.play();
});
四、总结
现在总结一下能够通用的方法(兼容ios和android)
; void function (win, doc, undefined) {
// 原理:模拟用户触发播放
Audio.prototype._play = Audio.prototype.play;
HTMLAudioElement.prototype._play = HTMLAudioElement.prototype.play; function wxPlayAudio(audio) {
WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
audio._play();
});
} function play() {
var that = this;
that._play();
try {
wxPlayAudio(that);
return;
} catch (e) {
document.addEventListener("WeixinJSBridgeReady", function(){
that._play();
}, false);
}
} Audio.prototype.play = play;
HTMLAudioElement.prototype.play = play;
}(window, document);
以上是自己的一些心得,如果有不对的地方希望能多多指教
微信的audio无法自动播放的问题的更多相关文章
- html5 audio标签微信部分苹果手机不能自动播放音乐终极解决方案
html5 audio标签微信部分苹果手机不能自动播放音乐终极解决方案 大家都知道需要在点击时候后 播放 ps:如果点击ajax 回来播放也不行,必须点击立即播放 要背景自动播放只能采取下面方案< ...
- 摇一摇—微信7.0.8版本audio无法自动播放问题
近日有一个项目出现audio无法自动播放,查看原因才发现是微信版本更新为7.0.8版本,需要有交互行为,第一次播放需要用户手动点击一下,无法使用DOM中的play()进行直接播放操作,那怎么办呢? 通 ...
- iphone微信 h5页音乐自动播放
iphone微信 h5页音乐自动播放: // iphone自动播放 document.addEventListener("WeixinJSBridgeReady", functio ...
- html5 video微信浏览器视频不能自动播放
html5 video微信浏览器视频不能自动播放 一.微信浏览器(x5内核): 1.不能自动播放 2.全屏 3.最顶层(z层的最顶层) 二.ios系统解决方案:(无phone手机未测试) <au ...
- egret 篇——关于ios环境下微信浏览器的音频自动播放问题
前段时间公司突然想用egret(白鹭引擎)做一个金币游戏,大半个月边看文档边写吭哧吭哧也总算是弄完了.期间遇到一个问题,那就是ios环境下微信浏览器的音频自动播放问题. 个人感觉吧,egret自己封装 ...
- 100%解决ios上audio不能自动播放的问题
由于ios的安全机制问题,不允许audio和video自动播放,所以想要使audio标签自动播放那是实现不了的,即使给play()也是播放不了. 解决方法: 首先,创建audio标签:<audi ...
- 微信video和audio无法自动播放解决方案
//音频,写法一<audio src="music/bg.mp3" autoplay loop controls>你的浏览器还不支持哦</audio> // ...
- 微信h5,背景音乐自动播放
移动端默认是禁止背景音乐自动播放的,很多需求都需要在页面加载完成的情况下同时出现背景音乐. 既然是微信h5,那么wx.config肯定不陌生,废话不多,直接上代码: html: <audio s ...
- 解决ios下的微信页面背景音乐无法自动播放问题
在做各种html5场景页面的时候,插入背景音乐是一个很普遍的需求,我们都知道,ios下的safari是无法自动播放音乐的,以至于现在行程一种认知,ios是没有办法自动播放媒体资源的,这个认知其实是错误 ...
随机推荐
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- ElasticSearch第四步-查询详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...
- Power Management开发的一般流程
本文作为一个提纲挈领的介绍性文档,后面会以此展开,逐渐丰富. 开发流程 针对一个PM feature进行开发,设计模型是第一步.模型设计好之后,还要保留参数接口,可以基于这些参数针对特殊个体进行优化. ...
- C#计算代码执行时间
System.Diagnostics.Stopwatch watch = new Stopwatch();watch.Start(); //要计算的操作 DoSomeThing(); watch.St ...
- Android样式和主题
样式:style--> 主题:theme--> <style name="my_style"> <item name="android:te ...
- jQuery种种
jquery UI autocomplete获得焦点自动弹出跟随下拉框--http://blog.csdn.net/jiusong_mi/article/details/49249853 $(&quo ...
- python 补漏计划
从今天开始把python的细枝末节都梳理下 争取 每星期 两篇博文
- 怎么在MindManager中查看打印预览
在MindManager2016思维导图中打印导图之前,可以先进行预览,MindManager和其他很多应用程序一样都带有打印预览功能,该功能提供了再次检查的机会,避免打印出错,MindManager ...