使用 Node.JS 播放音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12543237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play audio with Node.JS
提问by Znarkus
I'm currently using child_processand command-line mplayerto play audio on the local machine, with my Node.JS application. This works, but it's not really an excellent solution. My biggest issue is that it takes 500ms from mplayer is started to audio starts playing.
我目前正在使用child_process命令行在mplayer本地机器上使用我的 Node.JS 应用程序播放音频。这有效,但它并不是一个很好的解决方案。我最大的问题是从 mplayer 启动到音频开始播放需要 500 毫秒。
Are there better ways to play audio? Preferably compressed audio, but I'll take what I can get.
有没有更好的方法来播放音频?最好是压缩音频,但我会尽我所能。
采纳答案by saeed
I think what you asking is there any good modules that work with audio in the nodejs ecosystem?
我想你问的是 nodejs 生态系统中是否有与音频一起使用的好模块?
Whenever you have this type of question you should first go the npmjsand just type a appropiate keyword.
每当您遇到此类问题时,您应该首先访问npmjs并输入一个合适的关键字。
Here is list of modules that related to audioI found on the npmjs site.
这是我在 npmjs 站点上找到的与音频相关的模块列表。
substacks's baudiolooks good to me.
substacks 的baudio对我来说看起来不错。
回答by pedromtavares
I would suggest using node-speaker, which outputs raw PCM data to your speakers (so basically, it plays audio).
我建议使用node-speaker,它将原始 PCM 数据输出到您的扬声器(所以基本上,它播放音频)。
If you're playing something like mp3 files you might need to decode it first to PCM data, which is exactly what node-lamedoes.
如果您正在播放 mp3 文件之类的内容,您可能需要先将其解码为 PCM 数据,这正是node-lame所做的。
Hope that helps.
希望有帮助。
回答by andrewrk
Check out node-groove- Node.js binding to libgroove:
查看node-groove- Node.js 绑定到 libgroove:
This library provides decoding and encoding of audio on a playlist. It is intended to be used as a backend for music player applications, however it is generic enough to be used as a backend for any audio processing utility.
该库提供播放列表中音频的解码和编码。它旨在用作音乐播放器应用程序的后端,但它的通用性足以用作任何音频处理实用程序的后端。
Disclaimer: I wrote the library, which is free, open source, and not affiliated with any product, service, or company.
免责声明:我编写了该库,它是免费的、开源的,不隶属于任何产品、服务或公司。
回答by lipsumar
Simplest I've found (on Mac OS) is to use
我发现(在 Mac OS 上)最简单的是使用
exec('afplay whatever.mp3', audioEndCallback)
回答by Shubham Verma
You can use play-soundmodule also:
您也可以使用play-sound模块:
Install using npm, Run command:
使用 npm 安装,运行命令:
npm install play-sound --save
Now use in your code:
现在在您的代码中使用:
var player = require('play-sound')(opts = {})
player.play('./music/somebody20.flac', function (err) {
if (err) throw err;
console.log("Audio finished");
});
回答by Richie Bendall
Introducing, audic. It doesn't use any native dependencies and it's isomorphic so it can't break like in the answers higher up.
介绍,audic。它不使用任何本机依赖项并且它是同构的,因此它不会像更高级别的答案那样中断。
Observe:
观察:
const Audic = require("audic")
const audio = new Audic("audio.mp3")
// Play Audio
audio.play()
// Pause Audio
audio.pause()
回答by Harry Le
Check out this simple solution, it works on Windows and MacOS:
看看这个简单的解决方案,它适用于 Windows 和 MacOS:
const sound = require('sound-play')
sound.play('C:\music.mp3')

