Hello,
I have an integrated script on my website in RapidWeaver to be able to play a sound.
Does anyone have any idea how this could work?
Here is the website:
Thanks
Hi @aikonn,
Youâre giving us little information to go on
- What are you trying to do?
- When should it happen (on load, on a button click, something else?)
- Which script did you find and where did you put it? At quick glance, I donât see any inserted script in your siteâs source code.
Cheers,
Erwin
Thank you for your answer.
Oh! My original text has apparently disappeared.
So again:
I have an integrated script in a stack âHeader proâ
The script allows me to play a sound as soon as the page is opened. It also works very well. But I would like to change the script so that the sound only starts 1-2 seconds after the page has been clicked on. what do I have to change in the script?
Here is the script:
the script always disappears here in the forum, I donât know why. Here is a screenshot of the script:
Thank you
Hi @aikonn,
Hmmm, thatâs a good one.
Even though youâve used the script tag, youâre using the HTML5 autoplay feature. HTML5 doesnât have a feature to delay things to be parsed - HTML5 just parses the lines as it sees them, and builds up the page. When it reaches your audio autoplay tag, the browser will load its built-in audio player, load in the file that you specify, and play it.
You could change the script into JavaScript, which will give you a number of options to delay execution of code by a set number of time (or even variable amounts of time if you need it). For example, running the audio playbak inside a delay function, such as setTimeout().
In order to do so, remove the code from Header Pro, and instead drag in an HTML stack (found in your Stacks library, as it comes with Stacks as standard).
Delete the example HTML that comes standard in the HTML stack, and replace it with the following:
<audio id="delayedAudio" preload="auto">
<source src="%resource(sound.mp3)%" />
</audio>
<script>
window.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
const audio = document.getElementById("delayedAudio");
audio.play().catch(function(e) {
console.warn("Autoplay failed:", e);
});
}, 2000);
});
</script>
This code will load the file named âsound.mp3â located in Resources, but delays playback by 2 seconds. Change the â2000â to â1000â to make it 1 second.
Thereâs also a built in error message that will appear in the browserâs console should autoplay fail for some reason.
Cheers,
Erwin
Whow! That works. Thank you very much! Very nice.
Is it possible to hide the âmusic barâ?
I found out how to hide the music bar myself. I added:
div style=âvisibility:hiddenâ
exactly at the beginning of the script.
Now everything is just the way I wanted it. Thank you!
Good to hear @aikonn!
Cheers,
Erwin