PlaybackStatus in Mopidy node.js

I was wondering how to use the state described in the playbackStates documentation. According to the figure, there should be 3 states (playing, stopped, and paused), but the problem in my case is that when I use the PlaybackController’s getState()-method I get a state called pending. Is that another state not included in the state-diagram in the documentation?

I am also wondering how to use the PlaybackState in a node.js application. I want to do something like this:

if(mopidy.playback.getState() == mopidy.playback.state.STOPPED){
//do something
}

But I do not know how to use the class mopidy.core.PlaybackState in node. Any ideas?

Anyone who can help me with this one?
Maybe I should listen for events instead of asking for the state? Any feedback is appreciated.

Mopidy-MusicBox-Webclient uses Mopidy.js to make calls to the Mopidy core. I believe this is the canonical approach for HTTP clients, and may be a good place to start.

Some sample code for what I think you are trying to do is available here: https://github.com/pimusicbox/mopidy-musicbox-webclient/blob/7eea023310333dbfbae4bf1e846ef3dd8d029a7d/mopidy_musicbox_webclient/static/js/gui.js#L382

Subscribing to and responding on the events that Mopidy raises is generally a better approach than interrogating the core directly.

mopidy.playback.getState() is a function that have a async response:

mopidy.playback.getState().then(function(state){
// state is your state
});

:slight_smile:

Thank you for your replies. The answer provided are about how to use the getState() function, which is something I am doing as well. However, I want to do different things based on the state you are receiving, e.g the state is 'stopped' or 'playing' or anything else. Instead of:
if (state === 'stopped') //notice the quotes, the word may change in the future.
I would like to use the state variable which contains the value stopped.

Hello, i’m trying this simple code:

var Mopidy = require("mopidy");
var log=console.log;

var mopidy = new Mopidy({
    webSocketUrl: "ws://localhost:6680/mopidy/ws/"
});


mopidy.playback.getState().then(function(state){
  log (state);
});

but I get this error:

mopidy.playback.getState().then(function(state){
               ^

TypeError: Cannot read property 'getState' of undefined

Please advice, whats wrong?
Thank you

Hi!

mopidy.playback doesn’t exist before you’re connected to the server. If you wrap your code like this, so it doesn’t run until you’re connected, it should work:

mopidy.on('state:online', function () {
    mopidy.playback.getState().then(function (state) {
        console.log(state);
    });
});

See https://docs.mopidy.com/en/latest/api/js/ for the details.

ok, but how i can send mopidy.playback.play()?

can someone share example?
thank you

There’s an example of that in the docs: https://docs.mopidy.com/en/latest/api/js/#example-to-get-started-with