[HTML]音乐自动播放(兼容微信)
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<style type="text/css">
#play_btn {
position: absolute;
right: 6%;
top: 6%;
width: 24px;
height: 24px;
overflow: hidden;
background-color: #000;
border: solid 3px #ccc;
border-radius: 50%;
filter: alpha(opacity=50);
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
} #play_btn button {
display: inline-block;
width: 24px;
height: 24px;
background-image: url(music.png);
background-repeat: no-repeat;
background-position: center;
background-size: 45%;
background-color: transparent;
border: none;
} #play_btn audio {
width: 0px;
height: 0px;
overflow: hidden;
visibility: hidden;
} #play_btn.play {
-webkit-animation: play 3s linear infinite;
-moz-animation: play 3s linear infinite;
-ms-animation: play 3s linear infinite;
animation: play 3s linear infinite;
} #play_btn.pause {
-webkit-animation: none;
-moz-animation: none;
-ms-animation: none;
animation: none;
} @keyframes play {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
} @-moz-keyframes play {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
} @-webkit-keyframes play {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
} @-ms-keyframes play {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
</style>
</head> <body>
<div id="play_btn">
<button type="button"></button>
<audio src="Hebe-A_little_happiness.aac" loop preload></audio>
</div>
<script>
/*
touchstart,DOMContentLoaded无法在jQuery.ready里执行监听操作
*/
function autoPlay() {
/* 自动播放音乐效果,解决浏览器或者APP自动播放问题 */
function musicInBrowserHandler() {
audioToggle(true);
document.body.removeEventListener('touchstart', musicInBrowserHandler);
}
document.body.addEventListener('touchstart', musicInBrowserHandler); /* 自动播放音乐效果,解决微信自动播放问题 */
function musicInWeixinHandler() {
audioToggle(true);
document.addEventListener('WeixinJSBridgeReady', function() {
audioToggle(true);
}, false);
document.addEventListener('YixinJSBridgeReady', function() {
audioToggle(true);
}, false);
document.removeEventListener('DOMContentLoaded', musicInWeixinHandler);
}
document.addEventListener('DOMContentLoaded', musicInWeixinHandler);
}
autoPlay(); // ==================================================== function audioToggle(isPlay) {
var playBtn = document.getElementById('play_btn');
var audio = playBtn.getElementsByTagName('audio')[0]; if (typeof(isPlay) == 'undefined') {
isPlay = !!audio.paused;
} var space = String.fromCharCode(32); // 空格
var playBtnClass = space + (playBtn.getAttribute('class') || (isPlay ? 'play' : 'pause')) + space;
if (isPlay) {
playBtnClass = playBtnClass.replace(space + 'pause' + space, space + 'play' + space);
audio.play();
} else {
playBtnClass = playBtnClass.replace(space + 'play' + space, space + 'pause' + space);
audio.pause();
}
playBtn.className = playBtnClass.replace(/(^\s*)|(\s*$)/g, '');
} document.getElementById('play_btn').onclick = function() {
audioToggle();
}
</script>
</body> </html>
[HTML]音乐自动播放(兼容微信)的更多相关文章
- H5背景音乐自动播放(兼容微信IOS,进程后台切换自动停止播放,本文例子为Vue写法)
<template> <audio src="./static/music.mp3" id="bgMusic" preload="a ...
- iphone微信 h5页音乐自动播放
iphone微信 h5页音乐自动播放: // iphone自动播放 document.addEventListener("WeixinJSBridgeReady", functio ...
- ios微信浏览器音乐自动播放
setTimeout(function(){ //一般情况下,这样就可以自动播放了,但是一些奇葩iPhone机不可以 document.getElementById('bgmedia').play() ...
- H5音乐自动播放ios//禁止安卓手机图片点击
定义音乐按钮 <div id="music-btn" class="o-play" style="width: 24px; height: 24 ...
- html - html5 audio 音乐自动播放,循环播放等
转自:http://blog.csdn.net/u012246458/article/details/50618759 audio播放音乐 <audio id="audio" ...
- 解决ios下的微信打开的页面背景音乐无法自动播放
后面的项目发现,还有两个坑,需要注意下: ·本文的解决方案的核心是利用了 微信/易信 在ready的时候会有个 WeixinJSBridgeReady/YixinJSBridgeReady事件,通过监 ...
- 微信h5,背景音乐自动播放
移动端默认是禁止背景音乐自动播放的,很多需求都需要在页面加载完成的情况下同时出现背景音乐. 既然是微信h5,那么wx.config肯定不陌生,废话不多,直接上代码: html: <audio s ...
- 解决ios下的微信页面背景音乐无法自动播放问题
在做各种html5场景页面的时候,插入背景音乐是一个很普遍的需求,我们都知道,ios下的safari是无法自动播放音乐的,以至于现在行程一种认知,ios是没有办法自动播放媒体资源的,这个认知其实是错误 ...
- 解决在微信中部分IOS不能自动播放背景音乐
前言在做各种HTML5场景页面的时候,插入背景音乐是一个很普遍的需求.我们都知道,IOS下的safari是无法自动播放音乐的,以至一直以来造成一种错误的认识,iso是无法自动播放媒体资源的.直到微信火 ...
随机推荐
- python函数之可迭代对象、迭代器的判断
怎么判断一个对象是可迭代对象还是迭代器 例子 from collections import Iterable, Iterator lst = ['Today is Wednesday', 'Tomo ...
- LeetCode - Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- 芯灵思Sinlinx A64开发板Linux内核定时器编程
开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 Linux 内核定时器是内 ...
- Mybatis逆向工程的配置
源码github下载地址:https://github.com/wcyong/mybatisGeneratorCustom.git 参考文章:https://www.cnblogs.com/whgk/ ...
- 一键部署office的工具——OTool
OTool可用于office的下载.安装和激活,其激活方式是调用kmspico服务器进行的,官方网站是https://otp.landian.vip/zh-cn/,最新版本5.9.3.6在2019/4 ...
- 利用类的反射导入excel示例
直接上代码 1.页面展示 <div class="panel-heading font-bold text-center"> <i class="fa ...
- Vue 开发经验总结
Vue 开发经验总结 1.变量和方法,别写在全局,使用模块化导出.导入 对比项目 优点 缺点 适用场景 写在全局 会一直存在 使用模块化导出.导入 按需要导入 2.组件的prop值是请求接口后设置的( ...
- FileUpload上传
单文件上传: ASPX: <div> <!-- 文件上传 --> <asp:FileUpload ID="FileUpload1" runat=&qu ...
- DEX: Deep EXpectation of apparent age from a single image 论文阅读
来自:IMDB-WIKI - 500k+ face images with age and gender labels https://data.vision.ee.ethz.ch/cvl/rrot ...
- http请求返回响应码及意义
http 响应码及意义 HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.它由 RFC 2616 规范定义的,并得到RFC 2518.RFC 281 ...