+ 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.

6th Jan 2020, 12:48 AM
David
David - avatar
11 ответов
+ 2
David Martínez Castañón your question already has an answer in this thread https://www.sololearn.com/discuss/1921932/?ref=app
7th Jan 2020, 6:15 PM
Moses Odhiambo
Moses Odhiambo - avatar
+ 6
You can use the audio tag and a link.
6th Jan 2020, 2:43 AM
bobbie
bobbie - avatar
+ 1
Yes, for security reason autoplay is not allow!
6th Jan 2020, 3:59 AM
Thống Nguyễn
Thống Nguyễn - avatar
+ 1
Yes, that is what I was looking for. I had already searched it but didn't found anything. Thanks so much!!!
7th Jan 2020, 7:09 PM
David
David - avatar
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>
6th Jan 2020, 7:34 AM
Thống Nguyễn
Thống Nguyễn - avatar
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.
6th Jan 2020, 6:49 PM
David
David - avatar
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>
6th Jan 2020, 6:56 PM
David
David - avatar
0
Background music in website (especially autoplayed one) is usually considered as very bad user experience.
7th Jan 2020, 7:16 PM
Kacper Witas
Kacper Witas - avatar
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.
7th Jan 2020, 7:21 PM
David
David - avatar
0
David Martinez, My little music, you can try like that if you want : https://code.sololearn.com/WlPg4fbLjyzX/#html Enjoy !
24th Jan 2020, 7:23 AM
yadcubox
yadcubox - avatar