1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

getting this error in my angular webApp: NotAllowedError: play() failed because the user...

Discussão em 'Angular' iniciado por Test Office, Outubro 25, 2024 às 10:22.

  1. Test Office

    Test Office Guest

    I want to play notification sound in my angular website when receive message through socket. but getting this error- Audio play failed: NotAllowedError: play() failed because the user didn't interact with the document first.

    Here is my code sample -

    `export class SocketService {
    private isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
    private notification_sound!: HTMLAudioElement;

    constructor() {
    if (this.isBrowser) {
    this.notification_sound = new Audio('../../assets/sound/notification-souond-1.wav');
    }
    ...
    }

    socket.on('receive_message', (data) => {
    if (this.notification_sound) {
    this.notification_sound.play().then(() => {
    console.log("Audio played");
    }).catch((err) => {
    console.log("Audio play failed: ", err);
    });
    }
    });
    }`


    Please help...

    I need it play automatically when message is received.

    Continue reading...

Compartilhe esta Página