最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js audio player - Stack Overflow

programmeradmin0浏览0评论

I basically want to play a series of mp3-files after each other. It shouldn't be to hard, but I'm struggling to keep the decoder and speaker channel open to feed new mp3 data in after a song has been played. Here is a condensed version from what I have so far, playing one mp3 file.

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
});

I'm using TooTallNate's modules node-lame (for decoding) and node-speaker (for audio output through the speakers).

I basically want to play a series of mp3-files after each other. It shouldn't be to hard, but I'm struggling to keep the decoder and speaker channel open to feed new mp3 data in after a song has been played. Here is a condensed version from what I have so far, playing one mp3 file.

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
});

I'm using TooTallNate's modules node-lame (for decoding) and node-speaker (for audio output through the speakers).

Share Improve this question edited Feb 27, 2018 at 6:16 KARTHIKEYAN.A 20.2k10 gold badges137 silver badges150 bronze badges asked Jun 4, 2013 at 21:01 johnnyjohnny 9,0666 gold badges28 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

No experience whatsoever with the modules you mention, but I think you need to reopen the speaker each time you want to play a song (since you pipe the decoded audio to it, it will be closed once the decoder is done).

You could rewrite your code to something like this (untested);

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);

Another solution (one which I would prefer) is to use the very nice async module:

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();
  });
});
发布评论

评论列表(0)

  1. 暂无评论