Playing MP3 folder with JSON-RPC API?

Hi all,

I would like to play Spotify and simple MP3 folders with Mopidy and build an external script to interface with Mopidy. Now I want to create an extension out of it and start to internalize mpc calls.

For Spotify it’s working fine, for testing I changed from

mpc clear && mpc add "spotify:album:1xpGyKyV26uPstk1Elgp9Q" && mpc play

to

curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": { "uris": [ "spotify:album:1xpGyKyV26uPstk1Elgp9Q" ] } }' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc

For MP3s, so far I could play back my MP3 folder with mpc and the files were alphabetically sorted

mpc clear && mpc add "Files/folder1" && mpc play

With JSON-RPC this did not succeed for a folder, also not with an individual file.

curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": { "uris": [ "Files/folder1" ] } }' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": { "uris": [ "Files/folder1/file1.mp3" ] } }' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc

How can I play a folder with MP3s via JSON-RPC API?

While at at, I got two more quick questions:

  1. Is there a way to find out what Spotify playlist is playing? I only found album info.
  2. Is there a Mopidy SQLite database I can extend with my own table?

Thanks
bluepuma

Hi,

How can I play a folder with MP3s via JSON-RPC API?

For this you need to make multiple calls:

  1. Get the list of items with the browse endpoint
  2. Add the given track uris to the tracklist

If you want to recurse subdirectories, then you need to recurse the browse calls as well (for the File backend especially). The lookup endpoint seems to recurse automatically with the Local backend e.g, but not with the File backend.

Is there a way to find out what Spotify playlist is playing? I only found album info.

I don’t think that’s possible, mopidy takes the song from the playlist and adds them to the tracklist, then play the tracklist. There is no notion of current playlist.

Is there a Mopidy SQLite database I can extend with my own table?

I don’t think there is, but it’s not so difficult to make your own. I just implemented a very simple sqlite storage with an api, you can find the source files here and here.

Thanks, I didn’t know about the browse endpoint, I will check it out.

I figured out to play MP3 files directly via JSON-RPC API with the full local path. Either file:/home/pi/music/folder1/file1.mp3 or file:///home/pi/music/folder1/file1.mp3.

curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": { "uris": [ "file:/home/pi/music/folder1/file1.mp3" ] } }' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc

For persistent storage I found the nice SQLite wrapper for dicts. I thought if Mopidy has a database, I could just add a table to it.

Well the Local backend has a database, I’m not sure Mopidy does. But I guess you can use that db without issues yes.

I wouldn’t want to create an unnecessary dependency between my thing and mopidy-local. What if the next version of mopidy-local stops using an sqlite database and removes it?

Mopidy itself does not have a database.

Also, check you really need a database. Although Sqlite is nice to use in Python, storing/loading json with a file is also simple.