admin管理员组文章数量:1022964
i want to autoplay a mp3 file in jquery
i used this code,
function musicStart() {
pop = window.open('', 'back__music', 'width=100,height=100', true);
pop.document.write('<audio controls autoplay loop><source
src="./music.mp3"/></audio>');
}
loop is working, but autoplay isnt.
when i added autoplay="true"
or autoplay="1"
or autoplay="autoplay"
to audio tag, the results were the same.
or when i used embed tag, it doesn't repeatedly play the music.
i don't care using audio tag or embed tag
how can i do?
i want to autoplay a mp3 file in jquery
i used this code,
function musicStart() {
pop = window.open('', 'back__music', 'width=100,height=100', true);
pop.document.write('<audio controls autoplay loop><source
src="./music.mp3"/></audio>');
}
loop is working, but autoplay isnt.
when i added autoplay="true"
or autoplay="1"
or autoplay="autoplay"
to audio tag, the results were the same.
or when i used embed tag, it doesn't repeatedly play the music.
i don't care using audio tag or embed tag
how can i do?
Share Improve this question asked May 28, 2018 at 12:05 user9180012user9180012 311 gold badge1 silver badge5 bronze badges 1- You could maybe try to trigger playback by calling the node's play method on load. – soupy-norman Commented May 28, 2018 at 12:13
3 Answers
Reset to default 0$('audio')[0].play();
(assuming you only have one audio
in your page) should do the job.
A basic example:
$('audio')[0].play();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<audio controls>
<source src="http://media.w3/2010/07/bunny/04-Death_Bees_Fur.mp4" type='audio/mp4'></audio>
In jQuery you can achieve this as:
$('.music').html('<audio controls autoplay> <source src="http://media.w3/2010/07/bunny/04-Death_Bees_Fur.mp4" type="audio/mpeg"> </audio>');
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="music">
</div>
I know the solution !
I solved this way, if there is anyone that has the problem like me, follow me.
First, enter the link here~ chrome://flags/#autoplay-policy, and Check from Default to No user gesture is required
after then, change your code like this
function musicStart() {
pop = window.open('', 'back__music', 'width=100,height=100', true);
if (back__music.location.href === 'about:blank') {
pop.document.write('<audio controls autoplay loop><source src="./music.mp3" type="audio/mp3"/></audio>');
}
}
I wish hope with you :)
i want to autoplay a mp3 file in jquery
i used this code,
function musicStart() {
pop = window.open('', 'back__music', 'width=100,height=100', true);
pop.document.write('<audio controls autoplay loop><source
src="./music.mp3"/></audio>');
}
loop is working, but autoplay isnt.
when i added autoplay="true"
or autoplay="1"
or autoplay="autoplay"
to audio tag, the results were the same.
or when i used embed tag, it doesn't repeatedly play the music.
i don't care using audio tag or embed tag
how can i do?
i want to autoplay a mp3 file in jquery
i used this code,
function musicStart() {
pop = window.open('', 'back__music', 'width=100,height=100', true);
pop.document.write('<audio controls autoplay loop><source
src="./music.mp3"/></audio>');
}
loop is working, but autoplay isnt.
when i added autoplay="true"
or autoplay="1"
or autoplay="autoplay"
to audio tag, the results were the same.
or when i used embed tag, it doesn't repeatedly play the music.
i don't care using audio tag or embed tag
how can i do?
Share Improve this question asked May 28, 2018 at 12:05 user9180012user9180012 311 gold badge1 silver badge5 bronze badges 1- You could maybe try to trigger playback by calling the node's play method on load. – soupy-norman Commented May 28, 2018 at 12:13
3 Answers
Reset to default 0$('audio')[0].play();
(assuming you only have one audio
in your page) should do the job.
A basic example:
$('audio')[0].play();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<audio controls>
<source src="http://media.w3/2010/07/bunny/04-Death_Bees_Fur.mp4" type='audio/mp4'></audio>
In jQuery you can achieve this as:
$('.music').html('<audio controls autoplay> <source src="http://media.w3/2010/07/bunny/04-Death_Bees_Fur.mp4" type="audio/mpeg"> </audio>');
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="music">
</div>
I know the solution !
I solved this way, if there is anyone that has the problem like me, follow me.
First, enter the link here~ chrome://flags/#autoplay-policy, and Check from Default to No user gesture is required
after then, change your code like this
function musicStart() {
pop = window.open('', 'back__music', 'width=100,height=100', true);
if (back__music.location.href === 'about:blank') {
pop.document.write('<audio controls autoplay loop><source src="./music.mp3" type="audio/mp3"/></audio>');
}
}
I wish hope with you :)
本文标签: javascripthow to autoplay a music using audio tag in jqueryStack Overflow
版权声明:本文标题:javascript - how to autoplay a music using audio tag in jquery? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745520056a2154273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论