当前位置:网站首页>解决audio自动播放无效问题
解决audio自动播放无效问题
2022-06-23 09:55:00 【皮小孩ls】
问题描述
在360或者qq浏览器,audio的自动播放属性目前为止是有效的,网页的body里面插入下面代码后,打开页面就会自动播放音乐。
<audio autoplay="autoplay" loop="loop" preload="auto" src="http://music.163.com/song/media/outer/url?id=1337065812.mp3">
</audio>
但是Edge、谷歌等浏览器是不支持音乐自动播放的,通常会报错。
Uncaught (in promise) DOMException: play() failed because the user didn’t interact with the document first.
我们看一下谷歌浏览器怎么说的。(已自动翻译为中文)https://goo.gl/xX8pDD
当然下面还提出了一些解决办法,下面我会给出几个我已经测试过的解决办法。
解决办法1
上面也说了可以始终允许静音自动播放,用户可以通过点击按钮使其播放,这个方法很多平台都在使用。(比如网页浏览一个b站视频,需要点击播放音频按钮才会播放音频)
在body中插入如下代码即可。(按钮样式需要自己找一个)
<video id="video" muted autoplay src="http://music.163.com/song/media/outer/url?id=1337065812.mp3"></video>
<button type="button" id="unmuteButton">Click Me!</button>
<script> unmuteButton.addEventListener('click', function() {
video.muted = false; }); </script>
这个方法有一个问题就是音频一直在静音播放,你不知道进度,等你点击按钮可能音频已经播放几秒钟了。
解决办法2
使用网易云音乐的外链播放器,在网易云官网搜索一首歌曲之后进入播放页面,点击生成外链播放器。
使用iframe插件,复制代码。
但是要注意一点,代码中的src是不完整的,需要在前面加上http:
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="http://music.163.com/outchain/player?type=2&id=1464325108&auto=1&height=66" style="top:0px;position:absolute;z-index:2;left:0px"></iframe>
可以自己调整位置,让这个插件显示在页面上方,用户自己决定音乐的播放。
解决办法3
这个办法需要用户点击了页面才会自动播放音乐。
<audio id="bgmusic" src="http://music.163.com/song/media/outer/url?id=1851244378.mp3" autoplay="autoplay" loop="loop" style="display: block; width: 3%; height:3%;"></audio>
<script type="text/javascript"> function toggleSound() {
var music = document.getElementById("bgmusic");//获取ID console.log(music); console.log(music.paused); if (music.paused) {
//判读是否播放 music.paused=false; music.play(); //没有就播放 } } setInterval("toggleSound()",1); </script>
解决办法4
使用按钮控制HTML5背景音乐开关,源码来自https://www.helloweba.net/javascript/321.html
大家可以自行下载。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<title>演示:使用按钮控制HTML5背景音乐开关</title>
<style> </style>
<link rel="stylesheet" type="text/css" href="../css/main.css" />
</head>
<body>
<div id="main">
<div style="width:300px; height:120px; margin:100px auto">
<audio id="music2" src="http://music.163.com/song/media/outer/url?id=1337065812.mp3" loop="loop">你的浏览器不支持audio标签。</audio>
<a href="javascript:playPause();"><img src="pause.gif" width="48" height="50" id="music_btn2" border="0" style="top: 10px;right: 10px;position: absolute;"></a>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/1.8.2/jquery.min.js"></script>
<script> $("#audio_btn").click(function(){
var music = document.getElementById("music"); if(music.paused){
music.play(); $("#music_btn").attr("src","play.gif"); }else{
music.pause(); $("#music_btn").attr("src","pause.gif"); } }); function playPause() {
var music = document.getElementById('music2'); var music_btn = document.getElementById('music_btn2'); if (music.paused){
music.play(); music_btn.src = 'play.gif'; } else{
music.pause(); music_btn.src = 'pause.gif'; } } </script>
</body>
</html>
如何找音乐链接可以看我的文章:如何获取喜欢的音乐id
解决办法5
换个浏览器打开html文件,目前360、qq浏览器是支持自动播放音频的,不用任何操作。
边栏推荐
- Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
- Gorm advanced query
- After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs? How does the redo log handle this transaction
- NFTs、Web3和元宇宙对数字营销意味着什么?
- Cloud native database Amazon RDS
- oracle中遇到的bug
- 2021-04-15
- Centre de calcul haute performance - nvme / nvme - of - nvme - of overview
- thymeleaf中如何给onclick事件传值的方法
- What is BFC? What problems can BFC solve
猜你喜欢

Cesium loading orthophoto scheme

Unity技术手册 - 生命周期内速度限制(Limit Velocity Over Lifetime)子模块和速度继承(Inherit Velocity)子模块

Go语言JSON 处理

文件IO(1)

Go 单元测试

UEFI source code learning 3.7 - norflashdxe

Wechat applet: click the button to switch frequently, overlap the custom markers, but the value does not change

启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品

2021-05-07构造器

#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
随机推荐
High performance computing center RDMA implementation technology
2021-05-07构造器
NFTs、Web3和元宇宙对数字营销意味着什么?
J. Med. Chem. | Release: a new drug design model for deep learning based on target structure
马斯克 18 岁儿子请愿改名,欲断绝父子关系
Jog sport mode
天龙八部TLBB系列 - 网单服务端各目录文件说明【超详细】
MySQL optimistic lock and pessimistic lock
启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品
JSP getting started summary
Wechat applet: click the button to switch frequently, overlap the custom markers, but the value does not change
多线程习题
The usage of lambda of C
AI: the Elephant in Room
How does thymeleaf get the value of the request parameter in the URL?
AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
Getting started with cookies and sessions
Gorm advanced query
SQL教程之SQL 中数据透视表的不同方法