Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop a streaming mp3 #52

Open
jm21 opened this issue May 4, 2015 · 8 comments
Open

Stop a streaming mp3 #52

jm21 opened this issue May 4, 2015 · 8 comments

Comments

@jm21
Copy link

jm21 commented May 4, 2015

Is there anyway of stopping output to a speaker? Am streaming a mp3 from an online music service. The mp3 comes as a signed url. Am using sockets.io and the app is set up on raspberry. I want users to be able to play and stop the mp3's. Playing isn't a problem but stopping is. Below is my code

socket.on('playcontrol', function(data){
if(data.action == 'play'){

            console.log('playing');

            speaker = new Speaker();
            decoder = new lame.Decoder();

            stream = request(data.songurl);
            stream.pipe(decoder).pipe(speaker);

    }
     if(data.action == 'stop'){
       stream.on('data', function(chunk){
              stream.unpipe(speaker)
            })
        console.log('stopping')
     }
 })

I've tried various options like using throttle but no of them seem to work. Any idea what I can try?

@jkeylu
Copy link

jkeylu commented May 5, 2015

try this:

socket.on('playcontrol', function(data) {
  if (data.action == 'play') {
    console.log('playing');

    speaker = new Speaker();
    decoder = new lame.Decoder();

    stream = request(data.songurl);
    stream.pipe(decoder).pipe(speaker);
  }
  if (data.action == 'stop') {
    if (stream) {
      stream.unpipe();
    }
    console.log('stopping')
  }
})

@jm21
Copy link
Author

jm21 commented May 5, 2015

Thanks for the reply. I've tried your solution but its not working, actually doesn't make a difference. I've come up with the solution below which however is working erratically.

if (stream){
stream.pause()
speaker.close()
}

This works like 60% of the time. The other times it gives me the following error:

events.js:85
throw er; // Unhandled 'error' event
^
Error: write() failed: 3948
at onwrite (/var/www/test/node_modules/speaker/index.js:269:12)

Am assuming the error comes up because the speaker is trying to write data that doesn't exist. Is there a way of flushing out data first before closing the speaker? Any suggestions on what I can try?

@Edudjr
Copy link

Edudjr commented May 12, 2015

This is working for me:

    stream = fs.createReadStream(file);
    var decoder = new lame.Decoder();
    var spkr = new Speaker();
    decoder.pipe(spkr);
    stream.pipe(decoder);

    //Pause
    setTimeout(function(){
        //stream.unpipe(); - not sure if necessary
        spkr.end();
    }, 1000);

    //Resume
    setTimeout(function(){
        decoder.pipe(new Speaker());
    }, 5000);

But there is a problem, It takes more than 1 second to stop playing, and I don't know if this is the correct way of handling this

@Edudjr
Copy link

Edudjr commented May 12, 2015

Other possible solution:

    // Lame decoder & speaker objects
    var decoder = new lame.Decoder();

    // pipe() returns destination stream
    var spkr = decoder.pipe(new Speaker());

    // manually write data to the decoder stream,
    // which is a writeable stream
    stream = fs.createReadStream(file);

    stream.on('data', function (chunk) {
        decoder.write(chunk);
    });

    setTimeout(function(){
        decoder.unpipe();
    }, 1000);

    setTimeout(function(){
        decoder.pipe(spkr);
    }, 10000);

@RTK
Copy link
Contributor

RTK commented May 30, 2015

Unfortunately I only receive exceptions when I try any of your solutions:

unpipe(); TypeError: undefined is not a function

I get this error when using the unpipe, the pause or the stop approach (is not a function - error).

@Edudjr
Copy link

Edudjr commented May 31, 2015

@RTK Can you post your code?

@RTK
Copy link
Contributor

RTK commented May 31, 2015

I get my pcm data from ffmpeg, using fluent-ffmpeg:

var stream = ;// ffmpeg pcm data
var speaker = new Speaker();

stream.pipe(speaker);

setTimeout(function() {
speaker.unpipe(); // <- exception
}, 1000);

maybe because i'm using windows 8.1 ?

@Edudjr
Copy link

Edudjr commented May 31, 2015

@RTK You are not piping the speaker, that's why you are getting an error.

Try something like this:

var stream = ;// ffmpeg pcm data
stream.pipe(new Speaker());
setTimeout(function() {
    stream.unpipe(); 
}, 1000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants