node.jsring

Play an mp3 file on a wifi enabled soundbar using nodejs


I'm building some home automations. As part of this I have developed an API that look door bell presses, detects the faces of the person at the door. I have used Google's text to speech service for creating an mp3 file.

Now I would like to announce the name of the person to a Wi-Fi enabled speaker (Sonos beam gen1 in my case which doesn't have a bluethooth). How can I do that?


Solution

  • Found a solution for Sonos devices.

    import { SonosDevice } from "@svrooij/sonos";
    
    const sonos = new SonosDevice(`${process.env.SONOS_HOST}` || "192.168.0.*");
      try {
        // https://ringhosting.blob.core.windows.net/sonos/Passport2.pdf
        await sonos.PlayNotification({
          trackUri: `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/${process.env.AZURE_STORAGE_AUDIO_CONTAINER}/output.mp3`, // Can be any uri sonos understands
          // trackUri: 'https://cdn.smartersoft-group.com/various/someone-at-the-door.mp3', // Cached text-to-speech file.
          onlyWhenPlaying: false, // make sure that it only plays when you're listening to music. So it won't play when you're sleeping.
          timeout: 10, // If the events don't work (to see when it stops playing) or if you turned on a stream, it will revert back after this amount of seconds.
          volume: 40, // Set the volume for the notification (and revert back afterwards)
          delayMs: 700, // Pause between commands in ms, (when sonos fails to play notification often).
        });
        console.log("Notification played successfully!");
      } catch (e) {
        console.error(e);
      }