# How to play music from a local directory using node.js and mopidy?

**URL:** <https://discourse.mopidy.com/t/how-to-play-music-from-a-local-directory-using-node-js-and-mopidy/1088>\
**Category:** Misc\
**Created:** [January 29, 2016, 7:33pm UTC](https://discourse.mopidy.com/t/how-to-play-music-from-a-local-directory-using-node-js-and-mopidy/1088 "2016-01-29T19:33:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnedvard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/johnedvard/32/327_2.png) [@johnedvard](https://discourse.mopidy.com/u/johnedvard)\
**Post date:** [January 29, 2016, 7:33pm UTC](https://discourse.mopidy.com/t/how-to-play-music-from-a-local-directory-using-node-js-and-mopidy/1088/1 "2016-01-29T19:33:47Z")

</div>

Hi.  
I am wondering how I am able to play music from a local folder using mopidy and node.js.

I have installed mopidy, and I am able to get the server up and running when I type `mopidy` in the OSX’s terminal window.  
I have also made a node.js project, and I am able to connect to the mopidy server by doing the following:

```
var Mopidy = require('mopidy');

var mopidy = new Mopidy({
webSocketUrl:"ws://localhost:6680/mopidy/ws/",
callingConvention:"by-position-or-by-name"
});

```

I am also able to see if the mopidy s online, but I am not able to play any music.

```
mopidy.on("state:online", function () {
        console.log("mopidy is online! Lets try to play music");
        mopidy.playback.play();
        mopidy.playback.getState().then(function(res){
            console.log("result form getState",res); // prints stopped
        });
        mopidy.playback.getCurrentTrack().then(function(res){
            console.log("result form get current track",res); // prints null
        });

        mopidy.tracklist.getTracks().then(function(res){
            console.log("got tracks result",res); // prints []
        });
        mopidy.tracklist.getLength().then(function(res){
            console.log("got length result",res); // prints 0
        });

        mopidy.tracklist.getTlTracks().then( function (tracklist) {
           console.log("tracklist result",tracklist); //prints []
        });
        mopidy.playlists.asList().then(function(res){
            console.log("result for playlists",res); // prints []
        })
    });

```

my config file looks like this:

```
 [local]
    enabled = true
    media_dir = /Users/johnedvard/Dropbox/Musikk
    #scan_timeout = 1000
    playlists_dir = $XDG_DATA_DIR/mopidy/local/playlists
    tag_cache_file = $XDG_DATA_DIR/mopidy/local/tag_cache
    #excluded_file_extensions = 
    # .directory
    # .html
    # .jpeg
    # .jpg
    # .log
    # .nfo
    # .png
    # .txt

```

I run `mopidy local scan` and get the following:

```
INFO Starting Mopidy 1.1.2
INFO Loading config from builtin defaults
INFO Loading config from /Users/johnedvard/.config/mopidy/mopidy.conf
INFO Loading config from command line options
INFO Enabled extensions: mpd, http, stream, m3u, softwaremixer, file, local
INFO Disabled extensions: none
INFO Found 29 files in media_dir.
INFO Checking 22 tracks from library.
INFO Removing 0 missing tracks.
INFO Found 3 tracks which need to be updated.
INFO Scanning...
WARNING Failed local:track:.dropbox: The stream is of a different type than handled by this element.
WARNING Failed local:track:.DS_Store: No audio found in file.
WARNING Failed local:track:Icon%0D: Could not determine type of stream.
INFO Scanned 3 of 3 files in 0s.
INFO Done scanning.

```

then I run `mopidy` and get this:

```
INFO Starting Mopidy 1.1.2
INFO Loading config from builtin defaults
INFO Loading config from /Users/johnedvard/.config/mopidy/mopidy.conf
INFO Loading config from command line options
INFO Enabled extensions: mpd, http, stream, m3u, softwaremixer, file, local
INFO Disabled extensions: none
INFO Starting Mopidy mixer: SoftwareMixer
INFO Starting Mopidy audio
INFO Starting Mopidy backends: StreamBackend, M3UBackend, FileBackend, LocalBackend
INFO Loaded 0 M3U playlists from /Users/johnedvard/.local/share/mopidy/m3u
INFO Loaded 22 local tracks using json
INFO Audio output set to "autoaudiosink"
INFO Starting Mopidy core
INFO Starting Mopidy frontends: MpdFrontend, HttpFrontend
INFO MPD server running at [::ffff:127.0.0.1]:6600
INFO HTTP server running at [::ffff:127.0.0.1]:6680

```

Am I missing something in order to play music?  
Cheers

---

<div class="post-metadata">

**Author:** ![johnedvard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/johnedvard/32/327_2.png) [@johnedvard](https://discourse.mopidy.com/u/johnedvard)\
**Post date:** [January 30, 2016, 4:29pm UTC](https://discourse.mopidy.com/t/how-to-play-music-from-a-local-directory-using-node-js-and-mopidy/1088/2 "2016-01-30T16:29:20Z")

</div>

After playing around with the `API`, I finally figured it out. I had to use the `LibraryController` and search for the tracks that are located on the `"local" uri`, then add the tracks to the `TracklistController` in order to use the `PlaybackController` to play the musc. Phew 😅

```
var Mopidy = require("mopidy");
var mopidy = new Mopidy({
        webSocketUrl:"ws://localhost:6680/mopidy/ws/",
        callingConvention:"by-position-or-by-name"
    });
    mopidy.on("state:online", function () {
        console.log("mopidy is online! Lets try to play music");
        mopidy.library.search({"uri":["local"]}).then(function(res){
            mopidy.tracklist.add([res[0].tracks]);
            mopidy.playback.play(); // now we are playing music!!!!
        });
    });

```

If any of you have any comments about the code, or any tips on best practices and so forth, please tell 😀
