maephisto / youtube-audio-player

This module will help you play the audio of a YouTube video with Node.js
5 stars 1 forks source link

Failed to play YouTube video Error: write after end #1

Open willondubs opened 7 years ago

willondubs commented 7 years ago

Hi, Thanks for making this. I'm using it in an express route. It starts and stops great but I'm getting the "write after end" when starting again. Could you help me fix this? Here's my code:

var express = require('express'),
    router = express.Router(),
    request = require('request')
yap = require('youtube-audio-player');

router.get('/', function(req, res) {

    yap.play({
        url: vid.link
    });

    res.send('200');
})

router.get('/stop', function(req, res) {

    res.send('200');
    yap.stop();
})

Thanks!

Siimone commented 7 years ago

Hi, same problem here. I think that's a problem about streams. I found a solution. Just don't use this package, and use youtube-audio-stream (dependence of this package). You can save the stream in a variable, then .close() the stream. After than you can play another song etc.

const stream = require('youtube-audio-stream')
const decoder = require('lame').Decoder
const speaker = require('speaker')
var stream

var url_one = 'https://www.youtube.com/watch?v=hTWKbfoikeg'
var url_two = 'https://www.youtube.com/watch?v=VkvOLB7Yzhs'

stream = stream(url_one).pipe(decoder()).pipe(speaker())            
setTimeout(function(){
    stream.close()
    stream = stream(url_two).pipe(decoder()).pipe(speaker())
    setTimeout(function(){
            stream.close()   
    },15000)
},15000);
willondubs commented 7 years ago

Thanks, I'll try it. I tried youtube-audio-stream initially and couldn't stop the stream. For now, I'm using mopidy's youtube player. I'll revisit this.