0

I am searching for a way to auto play a music file at a specific time of the week.

E.g. on Mondays at 12 pm.

If that is not possible with weekdays, only hours would be also ok, like every day at 12 pm, but it would be great if it would work with weekdays.

After playing the file from start to end, it should stop and wait for the next turn, e.g. next Monday at 12pm.

I tried this code, but it didn't work for me:

JS:

<script>
var d = new Date();
var m = d.getMinutes();
var h = d.getHours();
if (h == 17 && m == 00){
      var sound = document.getElementById(sound1);
      sound.Play();
}
</script>

HTML:

<embed src="notify.mp3" autostart="false" width="0" height="0" id="sound1"
enablejavascript="true">

Thank you a lot in advance!!

Toto
  • 1
  • If you run that code between 5:59.59 and 6:00:00 (not inclusive) the sound will play. It will not however, play if you arrive before 6pm and wait until afterwards. You probably want to set a(n interval) timer to trigger some event once a second. Inside the handler for the timer, you can check the current time/day of week and when appropriate, play the file. `setInterval` would come in handy for such an approach. Of course, upon page-load you could work out how long it is until midday, use `setTimeout` to trigger a single event after the calculated time has elapsed. MDN has good JS help docs. – enhzflep Nov 29 '20 at 14:48

1 Answers1

0

You should use the <audio> HTML 5 element to play some song: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio

And if you want to sound to play at the right time, you should probably use a setTimeout when the page is loaded to play it at the next time slot.

Something like this (I haven't tested it):

<audio id="notifyAudio" src="notify.mp3" />
<script>
    // Our audio element
    const audio = document.getElementById("notifyAudio");

    // Retrieve the number of milliseconds between now and
    // the next alarm time (next time hour is 17:00:00)
    function getTimeBeforeNextAlarm() {
        // Current date
        const date = new Date();

        // If the current hour is already past 17,
        // then the next alarm will be the next day
        if (date.getHours() >= 17) {
          // Increase day of the month by one
          // (It automatically handles jumping to next month)
          date.setDate(date.getDate() + 1);
        }

        // Set the time to 17:00:00
        date.setHours(17);
        date.setMinutes(0);
        date.setMilliseconds(0);

        // Alarm time - Current time = Remaining time
        // (The Date is implicitly converted to a number of milliseconds)
        return date - Date.now();
    }

    // Schedule the next alarm
    function scheduleAlarm() {
        window.setTimeout(() => {
            // Play the audio
            audio.play();
            // Schedule the next alarm (in this case, in 24h)
            scheduleAlarm();
        }, getTimeBeforeNextAlarm());
    }

    scheduleAlarm();
</script>

Though, note that most browsers won't allow you to play a sound without a user interaction, unless the user has interacted a lot with your website already. More details here: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide

Noé Lebrun
  • 141
  • 3