C. Alle
C. Alle

Reputation: 21

Problem with the php "echo" function of an audio file in Firefox

I created a small function that plays me an audio file based on the number of notifications that a user has received. I put this feature in the wordpress footer.

Here is the code:

function custom_um_notification_sound() {
       $uno = "<audio src='https://www.example.com/note1itaen1.mp3' autoplay></audio>";
    $due = "<audio src='https://www.example.com/note1itaen2.mp3' autoplay></audio>";
    $tre = "<audio src='https://www.example.com/note1itaen3.mp3' autoplay></audio>";
    $quattro = "<audio src='https://www.example.com/note1itaen4.mp3' autoplay></audio>";
    $cinque = "<audio src='https://www.example.com/note1itaen5.mp3' autoplay></audio>";
    $new = "<audio src='https://www.example.com/note1itaenall.mp3' autoplay></audio>";
       $unread = (int)UM()->Notifications_API()->api()->get_notifications( 0, 'unread', true );

    if ( !is_user_logged_in() ) {
        return;
    }

   if (!isset($_COOKIE['notifaudio'])) {
        if ( $unread == 1 ) {
        echo $uno;
         setcookie("notifaudio", "notiaudio", time()+3600, COOKIEPATH, COOKIE_DOMAIN );
        }
        if ( $unread === 2 ) {
        echo $due;
          setcookie("notifaudio", "notiaudio", time()+3600, COOKIEPATH, COOKIE_DOMAIN );
        }
        if ( $unread === 3 ) {
        echo $tre;
          setcookie("notifaudio", "notiaudio", time()+3600, COOKIEPATH, COOKIE_DOMAIN );
        }
        if ( $unread === 4 ) {
        echo $quattro;
          setcookie("notifaudio", "notiaudio", time()+3600, COOKIEPATH, COOKIE_DOMAIN );
        }
        if ( $unread === 5 ) {
        echo $cinque;
          setcookie("notifaudio", "notiaudio", time()+3600, COOKIEPATH, COOKIE_DOMAIN );
        }
        if ( $unread > 5 ) {
        echo $new;
        setcookie("notifaudio", "notiaudio", time()+3600, COOKIEPATH, COOKIE_DOMAIN );
        }   
    } 
}
add_action( 'wp_footer', 'custom_um_notification_sound', 9999 );

The feature works perfectly in Chrome and Safari, but it doesn't work in Firefox. Can someone help me? Do you have any solution to solve the problem? Sorry for my English. Thank you all.

Upvotes: 0

Views: 93

Answers (1)

MisterCurtis
MisterCurtis

Reputation: 191

Firefox blocks all media with sound from playing automatically, by default. There is a browser option to allow autoplay but this isn't really a good solution to ask your visitors to do.

Other browsers will also block autoplay depending on the circumstances. Chrome and WebKit for example have their own policies. This usually requires some user interaction before autoplay is allowed.

I'd suggest reading through Autoplay guide for media and Web Audio APIs on MDN to gain some context on the current barriers and limitations for autoplay, and some possible workarounds.

Upvotes: 2

Related Questions