Node.js音频播放器
Node.js audio player
我基本上想一个接一个地播放一系列mp3文件。这应该不难,但我一直在努力保持解码器和扬声器通道打开,以便在一首歌播放后输入新的mp3数据。以下是我目前收集到的压缩版本,播放一个mp3文件。
var audioOptions = {channels: 2, bitDepth: 16, sampleRate: 44100};
// Create Decoder and Speaker
var decoder = lame.Decoder();
var speaker = new Speaker(audioOptions);
// My Playlist
var songs = ['samples/Piano11.mp3','samples/Piano12.mp3','samples/Piano13.mp3'];
// Read the first file
var inputStream = fs.createReadStream(songs[0]);
// Pipe the read data into the decoder and then out to the speakers
inputStream.pipe(decoder).pipe(speaker);
speaker.on('flush', function(){
// Play next song
});
我正在使用TooTallNate的模块node-lame(用于解码)和node-speaker(用于通过扬声器输出音频)。
没有任何经验与你提到的模块,但我认为你需要重新打开扬声器每次你想播放一首歌(因为你管道解码音频到它,它将关闭一旦解码器完成)。
你可以重写你的代码像这样(未经测试);
var audioOptions = {channels: 2, bitDepth: 16, sampleRate: 44100};
// Create Decoder and Speaker
var decoder = lame.Decoder();
// My Playlist
var songs = ['samples/Piano11.mp3','samples/Piano12.mp3','samples/Piano13.mp3'];
// Recursive function that plays song with index 'i'.
function playSong(i) {
var speaker = new Speaker(audioOptions);
// Read the first file
var inputStream = fs.createReadStream(songs[i]);
// Pipe the read data into the decoder and then out to the speakers
inputStream.pipe(decoder).pipe(speaker);
speaker.on('flush', function(){
// Play next song, if there is one.
if (i < songs.length - 1)
playSong(i + 1);
});
}
// Start with the first song.
playSong(0);
另一个解决方案(我更喜欢的一个)是使用非常好的async模块:
var async = require('async');
...
async.eachSeries(songs, function(song, done) {
var speaker = new Speaker(audioOptions);
var inputStream = fs.createReadStream(song);
inputStream.pipe(decoder).pipe(speaker);
speaker.on('flush', function() {
// signal async that it should process the next song in the array
done();
});
});
相关文章:
- 动态加载的自定义javascript/jQuery/HTML5音频播放器的问题
- Javascript::通过HTML5音频播放器播放列表播放多个音频文件
- HTML5、CSS3和/或JS中旋转CD上的音频播放器
- 想制作一个基于URL的音频播放器,但Javascript不起作用
- 使用javascript播放音频(没有HTML5或可见播放器)
- 多个音频播放器和 Web 音频 API
- HTML5音频播放器只有在双击后才能在移动浏览器上播放
- 为什么我的代码使用javascript与html5音频播放器可以'Don’我不能做两个或两个以上
- 如何用一个html5音频播放器播放多个音频文件
- JS音频播放器,点击列表项即可播放
- jQuery:支持几乎所有格式的音频播放器
- 从我自己的按钮启动音频播放器
- 浏览器内音频播放器如何工作
- 如果未找到动态音频 src 或为 null,请使用 javascript 隐藏 HTML5 音频播放器
- 在HTML音频播放器中切换播放/暂停按钮
- 为音频播放器创建曲目标题(可在代码笔上找到)
- 使用javascript和jquery将“下一个”和“上一个”按钮添加到来自JSON的HTML5音频播放器
- 苹果电视的音频播放器 - TVML
- 如何为音频播放器创建可视化工具
- 超级简单的音频闪存播放器