简体   繁体   English

如何在我的 node.js 聊天室中播放声音?

[英]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.我整天都在寻找如何用 node.js 播放声音,我不能使用“document.getElementById”,也不能使用“new Audio”。 I want it to be able to play sound when I do @everyone in my chatroom.我希望它能够在我在聊天室中 @everyone 时播放声音。 The audio file is name "ping.mp3" and is in the same path as my main node.js file.音频文件名为“ping.mp3”,与我的主 node.js 文件位于同一路径。 I need some recommendations or code snippets.我需要一些建议或代码片段。 Thanks!谢谢!


This is a code snippet of where the ping code is.这是 ping 代码所在位置的代码片段。

  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();"我想要“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:您可以使用play-sound为您处理它:

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:它基本上所做的是在环境中寻找声音播放器并尝试使用其中一个播放 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.如果您正在创建 node.js 服务器,node.js 是后端/服务器端,因此如果在那里“播放”声音,用户将听不到任何发生的事情。 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. ).因此,您必须使用“new Audio()”在客户端进行操作,当客户端收到包含“@everyone”的消息时(我不明白您为什么不能使用,您可以将 mp3 文件发送给客户端与页面。 )。 I also made an example for you on repl .我还在repl上为你做了一个例子。

This is not an answer, this is just a code snippet of where the ping code is.这不是答案,这只是 ping 代码所在位置的代码片段。

  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();"我想要“ping.play();” to make the sound.发出声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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