问题
I'm currently using child_process and command-line mplayer
to 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.
Are there better ways to play audio? Preferably compressed audio, but I'll take what I can get.
回答1:
I think what you asking is there any good modules that work with audio in the nodejs ecosystem?
Whenever you have this type of question you should first go the npmjs and just type a appropiate keyword.
Here is list of modules that related to audio I found on the npmjs site.
substacks's baudio looks good to me.
回答2:
I would suggest using node-speaker, which outputs raw PCM data to your speakers (so basically, it plays audio).
If you're playing something like mp3 files you might need to decode it first to PCM data, which is exactly what node-lame does.
Hope that helps.
回答3:
Simplest I've found (on Mac OS) is to use
exec('afplay whatever.mp3', audioEndCallback)
回答4:
Check out node-groove - Node.js binding to 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.
回答5:
You can use play-sound
module also:
Install using npm, Run command:
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");
});
回答6:
Introducing, audic. It doesn't use any native dependencies and it's isomorphic so it can't break like in the answers higher up.
Observe:
const Audic = require("audic")
const audio = new Audic("audio.mp3")
// Play Audio
audio.play()
// Pause Audio
audio.pause()
回答7:
Check out this simple solution, it works on Windows and MacOS:
const sound = require('sound-play')
sound.play('C:\\music.mp3')
来源:https://stackoverflow.com/questions/12543237/play-audio-with-node-js