+ 2
[SOLVED] How can I play background music in HTML?
I have already tried autoplay, but I think it doesn't work on the sololearn app.
11 Antworten
+ 2
David Martínez Castañón your question already has an answer in this thread
https://www.sololearn.com/discuss/1921932/?ref=app
+ 6
You can use the audio tag and a link.
+ 1
Yes, for security reason autoplay is not allow!
+ 1
Yes, that is what I was looking for. I had already searched it but didn't found anything. Thanks so much!!!
0
David Martínez Castañón you can try this little trick to reach the same effect:
$(document).ready(function(){
$(document).on("mouseover", function(){
$("audio").trigger("play");
$(document).off("mouseover");
});
});
Note: above code using jQuery, so you need to put this code in your <head></head> part:
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
0
Ok, thank you all! I tried but it seems that there's no way to play a song without a user's gesture. At the end I made a button to show the content and play the song using javascript.
0
The code will be something like this:
Javascript
var music = new Audio(audio.mp3);
HTML
<body>
<button onclick="music.play()"> Play song </button>
</body>
0
Background music in website (especially autoplayed one) is usually considered as very bad user experience.
0
Yeah, I know. I was just trying to make an animation in wich music starts automatically. I dont like bg music in websites either.
0
David Martinez, My little music, you can try like that if you want :
https://code.sololearn.com/WlPg4fbLjyzX/#html
Enjoy !