简体   繁体   中英

How do I play sound in my node.js chatroom?

I have been looking up how to play sound with node.js all day, I can't use "document.getElementById" and I can't use "new Audio" either. I want it to be able to play sound when I do @everyone in my chatroom. The audio file is name "ping.mp3" and is in the same path as my main node.js file. I need some recommendations or code snippets. Thanks!


This is a code snippet of where the ping code is.

  function highlight(message){
    if(message == "") {
        return message
    }
    let mentions = message.match(/@\b([A-Za-z0-9]+)\b/g)
    let urlCheck1 = message.split(` `)
    
    if (mentions === null ) { return message }
    for (i = 0; i < mentions.length; i++) {
      let urlCheck = urlCheck1[i].includes(`http`)
        let mention = mentions[i].substring(1)
        if(sesskx.has(mention) && !urlCheck) {
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        } else if (mention == 'everyone') {
            ping.play();
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        } else if (mention == 'here') {
            ping.play();
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        }
        else {
          return message;
        }
    }
    return message
 };

I want "ping.play();" to make the sound.

This is possible but kind of tricky. You can use play-sound to take care of it for you:

var player = require('play-sound')(opts = {})
 
player.play('foo.mp3', function(err){
  if (err) throw err
});

What it basically does is look for sound players in the environment and try to use one of them to play the mp3:

const { spawn, execSync } = require('child_process');
// List of players used
const players = [
    'mplayer',
    'afplay',
    'mpg123',
    'mpg321',
    'play',
    'omxplayer',
    'aplay',
    'cmdmp3'
];
// find a player by seeing what command doesn't error
let player;
for(const p of players) {
  if (isExec(p)) {
    player = p;
    break;
  }
}

function isExec(command) {
  try{
    execSync(command)
    return true
  }
  catch {
    return false
  }
}
spawn(player, ['ping.mp3']);

If you are creating a node.js server, node.js is back-end/server side, so users won't be able to hear anything that happens if a sound is "played" there. So you'll have to do it client side using "new Audio()", when the client receives a message including "@everyone" (which I don't understand why you cant use, you can send the mp3 file to the client with the page. ). I also made an example for you on repl .

This is not an answer, this is just a code snippet of where the ping code is.

  function highlight(message){
    if(message == "") {
        return message
    }
    let mentions = message.match(/@\b([A-Za-z0-9]+)\b/g)
    let urlCheck1 = message.split(` `)
    
    if (mentions === null ) { return message }
    for (i = 0; i < mentions.length; i++) {
      let urlCheck = urlCheck1[i].includes(`http`)
        let mention = mentions[i].substring(1)
        if(sesskx.has(mention) && !urlCheck) {
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        } else if (mention == 'everyone') {
            ping.play();
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        } else if (mention == 'here') {
            ping.play();
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        }
        else {
          return message;
        }
    }
    return message
 };

I want "ping.play();" to make the sound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM