当前位置:网站首页>Solve the problem of invalid audio autoplay
Solve the problem of invalid audio autoplay
2022-06-23 10:26:00 【Skin kid LS】
Catalog
Problem description
stay 360 perhaps qq browser ,audio The autoplay attribute of is currently valid , Web page body After inserting the following code , Open the page and the music will be played automatically .
<audio autoplay="autoplay" loop="loop" preload="auto" src="http://music.163.com/song/media/outer/url?id=1337065812.mp3">
</audio>
however Edge、 Google and other browsers do not support automatic music playback , It's usually wrong .
Uncaught (in promise) DOMException: play() failed because the user didn’t interact with the document first.
Let's take a look at what Google Chrome says .( Automatically translated to Chinese )https://goo.gl/xX8pDD
Of course, some solutions are proposed below , Here are a few solutions I've tested .
terms of settlement 1
It is also said that you can always allow silent automatic playback , The user can go through Click the button to make it play , This method is used by many platforms .( For example, browsing a web page b Stop video , You need to click the play audio button to play the audio )
stay body The following code can be inserted .( You need to find a button style )
<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>
One problem with this method is that the audio is always playing silently , You don't know the progress , When you click the button, the audio may have played for a few seconds .
terms of settlement 2
Use the external chain player of Netease cloud music , Search a song on the official Netease cloud website and enter the play page , Click to generate external player .
Use iframe plug-in unit , Copy code .
But be careful , In code src It's incomplete , You need to add 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>
You can adjust the position by yourself , Let the plug-in appear at the top of the page , The user decides how to play the music .
terms of settlement 3
This approach requires users Click the page to automatically play music .
<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");// obtain ID console.log(music); console.log(music.paused); if (music.paused) {
// Interpret whether to play music.paused=false; music.play(); // Play without } } setInterval("toggleSound()",1); </script>
terms of settlement 4
Use button control HTML5 Background music switch , Source code from https://www.helloweba.net/javascript/321.html
You can download it yourself .
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<title> demonstration : Use button control HTML5 Background music switch </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"> Your browser does not support it audio label .</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>
How to find music links can see my article : How to get your favorite music id
terms of settlement 5
Open another browser html file , at present 360、qq The browser supports automatic audio playback , Without any operation .
边栏推荐
- 【第23天】给定一个长度为 n 的数组,将元素 X 插入数组指定的位置 | 数组插入操作4
- 解决audio自动播放无效问题
- Too helpless! Microsoft stopped selling AI emotion recognition and other technologies, saying frankly: "the law can not keep up with the development of AI"
- NOI OJ 1.3 11:计算浮点数相除的余数 C语言
- Lying trough, the most amazing paper artifact!
- Shengshihaotong enables high-quality development with industrial Digitalization
- 2021-05-07 package inheritance super this
- File IO (1)
- 2021-04-12 the first implementation of linked list!!!
- 135,137,138,445禁用导致无法远程连接数据库
猜你喜欢

解决预览pdf不能下载的问题

Build the information and innovation industry ecology, and make mobile cloud based on the whole stack of independent innovation

Golang quick start (3)

构建信创产业生态,移动云立足全栈自主创新连放大招

R和RStudio下载安装详细步骤

【软件与系统安全】堆溢出

Build a QQ robot to wake up your girlfriend

Cool photo album code, happy birthday to the object!

2021-05-07 constructor

Go language JSON processing
随机推荐
After installing these five plug-ins, pycharm can be used for takeoff!
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
MySQL-01.工作中数据库优化和explain字段知识的总结
SQL writing problem to calculate the ring ratio of the current month and the previous month
2021-04-12 the first implementation of linked list!!!
NOI OJ 1.3 15:苹果和虫子 C语言
How to pass values to onclick events in thymeleaf
oracle中遇到的bug
What is JSX in the JS tutorial? Why do we need it?
Gorm advanced query
MySQL-02.工作中对索引的理解
NOI OJ 1.3 04:带余除法 C语言
太无奈!微软停售 AI 情绪识别等技术,直言:“法律跟不上 AI 的发展”
NOI OJ 1.2 整数数据类型存储空间大小
搭建一个QQ机器人叫女友起床
【第23天】给定一个长度为 n 的数组,将元素 X 插入数组指定的位置 | 数组插入操作4
2021-04-16 recursion
2021-05-07构造器
谷贱伤农,薪贱伤码农!
Unable to enter the system normally, press F8 to select other items to try to enter