D
D
dez64ru2019-05-07 20:53:44
JavaScript
dez64ru, 2019-05-07 20:53:44

How to play a notification sound on a site without asking for autoplay?

For some time now, browsers have been blocking autoplay of sounds.
However, my research on instant messengers showed that none of them asks for the rights to automatically play sound.
So how can I do the same on my site?
I make notifications.
A piece of test code in order to understand

$(function () {
            let audio = new Audio();
            audio.preload = 'auto';
            audio.src = '/wp-content/uploads/2019/05/notif_bell.mp3';

            let notif_list = [
                {
                    title: 'Тестовый уведомление №1',
                    description: 'Просто описание уведомлениа №1',
                    date: {
                        start: <?= date($time_format, time()); ?>,
                        end: <?= date($time_format, time() + 60 * 30); ?>
                    },
                    played: false,
                },
                {
                    title: 'Тестовый уведомление №2',
                    description: 'Просто описание уведомлениа №2',
                    date: {
                        start: <?= date($time_format, time() + 60); ?>,
                        end: <?= date($time_format, time() + 60 * 31); ?>
                    },
                    played: false,
                }
            ];

            let $modal = $('#rek-notif');

            let isPaused = false;

            $modal.on('click', '.click', function () {
                $modal.fadeOut('500');
                isPaused = false;
            });

            audio.onloadeddata = function () {

                setInterval(function () {
                    let ts = Math.floor(new Date() / 1000);

                    $.each(notif_list, function (i, notif_item) {
                        if (isPaused) {
                            return;
                        }

                        if (!isPaused && !notif_item.played && ts >= notif_item.date.start && ts <= notif_item.date.end) {

                            let promise = audio.play();

                            if (promise !== undefined) {
                                promise.then(_ => {

                                    isPaused = true;
                                    notif_list[i].played = true;

                                    $modal.find('.title').text(notif_list[i].title);
                                    $modal.find('.description').text(notif_list[i].description);

                                    $modal.fadeIn('500');
                                }).catch(error => {
                                    // Autoplay was prevented.
                                    // Show a "Play" button so that user can start playback.
                                });
                            }

                            setTimeout(function () {
                                $modal.fadeOut('500');
                                isPaused = false;
                            }, 10000);
                        }
                    });
                }, 1000);
            };
        })

Okay chrome, there until the user starts interacting with the page.
But the fox is completely blocking.
How to solve this problem?
(Errors in the string data of the code and classes (id) may be due to autocorrect purely for the toaster)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question