4

How can I add a sound to a lightning button in LWC?

import { LightningElement } from 'lwc';
const audio = new Audio("https://www.fesliyanstudios.com/play-mp3/387");

export default class GetRandomQuotes extends LightningElement {
    sound = audio;
    onClickHandler(){
        this.sound.play();
    }
}
1
  • Welcome to Salesforce Stack Exchange (SFSE)! Please edit your question to add what happens (e.g., exact error text, exact unexpected results) when you run the above code.
    – Moonpie
    Nov 7, 2022 at 12:47

1 Answer 1

9

What you're likely experiencing is a limitation imposed by Salesforce CSP. In order to make it work, you need to Manage CSP Trusted Sites. Go to Setup | CSP Trusted Sites, and add the site to enable access to the media files. Outside of Salesforce, LWC has no problem loading and playing the click sound.

Demo.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .