微信的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是没有办法自动播放媒体资源的,这个认知其实是错误 ...
随机推荐
- [开源].NET数据库访问框架Chloe.ORM
扯淡 13年毕业之际,进入第一家公司实习,接触了 EntityFramework,当时就觉得这东西太牛了,访问数据库都可以做得这么轻松.优雅!毕竟那时还年轻,没见过世面.工作之前为了拿个实习机会混个工 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 类EF框架Chloe.ORM升级:只为更完美
扯淡 Chloe.ORM:一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接 ...
- C++远征之封装篇(下)
对象数组 类 x1[]; 栈中实例化,不用delete. 类 *X=new X[];//在堆中实例化,结尾需要用delete删除 delete []X; //这是数组的删除形式 X=NULL;
- hihoCoder太阁最新面经算法竞赛15
hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...
- C#中的WebBrowser控件的使用
0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...
- HYSBZ 2038 莫队算法
小Z的袜子(hose) Time Limit:20000MS Memory Limit:265216KB 64bit IO Format:%lld & %llu Submit ...
- [展示]手把手教你如何diy门户幻灯片
第一步后台新建页面:这个就不用说了大家都会 新建后FTP里面会出现如下一个模板页面 第二步从ftp里面下载 template的index.htm文件 给首页模板页面添加JS代码 如下 将这段jS ...
- cocos2d-x3.x自定义事件
-- 自定义事件 -- 监听: local eventDispatcher = self:getEventDispatcher();--self为继承Node的对象 local function ha ...