How to play a custom sound in Electron

Viewed 1007

I am using Electron 8.0.3 and I am trying to play a custom sound. Here's what I am doing:

  const notif = new Notification({
    title: 'Finished Download',
    body: 'test',
    sound: 'vapp/assets/sounds/mighty_sound.mp3',
  });

  notif.show();

It doesn't seem to play that sound but instead a default macOS sound. I've tried:

  • Using a absolute path like '/Users/<name>/Desktop/workspace/proj/vapp/assets/sounds/sound.mp3'
  • Packaging the application so that the sound is bundled
  • Playing different file types: .wav, .mp3, .aiff
  • Choosing other macOS sounds that might exist in /System/Library/Sounds

For whatever reason, it plays the same sound.

I have referenced this documentation

1 Answers

My approach was to set the silent attribute of electron's Notification module to true so that the OS sound doesn't play and then use this sound-play npm package to play my own sound.

const showNotification = () => {
  new Notification({
    title: "Elon Musk is Tesla CEO",
    body: "The automaker just got...",
    silent: true, //// Disable sound by operating system
  }).show();

  // Play custom sound
  const sound = require("sound-play");
  sound.play("./src/quite-impressed.mp3");
};
Related