Code Monkey home page Code Monkey logo

Comments (6)

JSteunou avatar JSteunou commented on August 27, 2024

I might be concern with this issue. I tried to made a simple web ui for my own needs, with 2 routes, one launching a cast on stream url, the other stopping the player.

It goes like this

var debug = require('debug')('webui');
var express = require('express');
var Player = require('chromecast-player');
var router = express.Router();



router.get('/launch', function(req, res, next) {
    var media = req.query.media;
    if (!media) return next(new Error('Missing media query param'));

    var options = {
        path: media,
        type: req.query.type
    };

    var player = new Player();
    player.attach(function(err, p) {
        if (err) return next(err);

        debug('launch player %o', options);
        p.load(options, function(err) {
            if (err) return next(err);
            res.status(200).send();
            // process.exit();
        });

    });
});

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

    var player = new Player();
    player.attach(function(err, p) {
        if (err) return next(err);

        debug('stop player');
        p.stop(function(err) {
            if (err) return next(err);
            res.status(200).send('ok');
            // process.exit();
        });
    });

});



module.exports = router;

But if I call my routes twice (like launch then stop) I got this error

device not found

Error: device not found
    at null._onTimeout (/home/jsteunou/git/castnow-webui/node_modules/chromecast-player/node_modules/chromecast-scanner/index.js:34:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

You can see I tried to find a way around the error by killing the process after each success, letting my watchdog starting back the server. But that does not work well either.

What can I do to help?

from chromecast-player.

xat avatar xat commented on August 27, 2024

The player instance now can be destroyed like this:

player.launch('<url>', function(err, p, ctx) {
    setTimeout(function() {
        ctx.shutdown();
    }, 5000);

    ctx.on('closed', function() {
        console.log('player got shutdown');
    });
});

You should now also be able to launch the player multiple times, as long as you shutdown the instance which was running before.

from chromecast-player.

JSteunou avatar JSteunou commented on August 27, 2024

thanks a lot, I'll try that!

from chromecast-player.

JSteunou avatar JSteunou commented on August 27, 2024

Ok my code is a lot cleaner now, I did not have to call that shutdown method, or kill my process like before, and it works beautifully well!

var debug = require('debug')('webui');
var express = require('express');
var Player = require('chromecast-player');
var router = express.Router();



router.get('/launch', function(req, res, next) {
    var media = req.query.media;
    if (!media) return next(new Error('Missing media query param'));

    var options = {
        path: media,
        type: req.query.type
    };

    debug('launch player with options: %o', options);
    var player = new Player();
    player.launch(options, function(err, p, ctxt) {
        if (err) return next(err);

        p.once('playing', function(info) {
            debug('playing %o', info);
            res.status(200).send('ok');
        });

    });
});

router.get('/stop', function(req, res, next) {
    debug('attach to player');
    var player = new Player();
    player.attach(function(err, p, ctxt) {
        if (err) return next(err);

        debug('stop player');
        p.stop(function(err, info) {
            if (err) return next(err);
            debug('stopped %o', info);
            res.status(200).send('ok');
        });
    });
});



module.exports = router;

again thank you.

from chromecast-player.

xat avatar xat commented on August 27, 2024

Nice, but you should run the shutdown() method even if it works without. shutdown() will unlisten eventListeners and stop the setInterval created by the timeline helper. Otherwise it will leek memory.

from chromecast-player.

JSteunou avatar JSteunou commented on August 27, 2024

ho ok thanks for the advice.

from chromecast-player.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.