MPD commands to select stream

HI Guys I am hoping someone can help me, I am using the python MPDClient to control Pi Musicbox. I am happily able to switch between spotify playlists and control volume but I would like to be able to switch between web streams. I was wondering if anyone had managed this and what commands they used.
Thanks
Steve

Yes, you can do that from python or any other place by calling the the JSON-RPC API via HTTP (see here: https://docs.mopidy.com/en/release-0.19/api/http/)

In order to start a stream I first add a new Stream to the tracklist and then call the play command for that new track in a second request.
I’m using the “Python Requests” package as HTTP client.

url = http://<HOST_NAME>:6680/mopidy/rpc
headers = {'Content-Type': 'application/json'}
payload = """{
	 "method": "core.tracklist.add",
	"jsonrpc": "2.0",
	"params": {
	"tracks": null,
	"at_position": null,
	"uri": "http://mp3stream1.apasf.apa.at:8000"
	 },
	"id": 1
}"""

r = requests.post(url, headers=headers, data=payload)
if r.status_code == 200:
    result = r.text
    jsonResponse = json.loads(result)

    if "result" in jsonResponse and len(jsonResponse["result"]) > 0:
        trackInfo = jsonResponse["result"][0]

         payload = """{
              "method": "core.playback.play",
              "jsonrpc": "2.0",
              "params": {
                  "tl_track": %s,
                  "on_error_step": -1
                },
                "id": 1
             }""" % json.dumps(trackInfo)

         r = requests.post(url, headers=headers, data=payload)

Hope this helps

Jochen

Hello,

Is there a way that allow you to just “Play” a uri stream, without adding it to a tracklist ?

I’m afraid that if for example I play stream1 then stream2 and then stream1 again, etc… One day my tracklist will be 1km long…

but anyway, thank you for this tip. I’ll try with an ESP8266 to play streams.

No, you must add to the tracklist.

If you enable consume mode the track will be removed from the tracklist once you have played it (or skipped it). If you are only alternating between a few streams then enable repeat mode, this will start playing the first track again once you’ve played/skipped the last one.

Hello,

the solution above does’nt work for me and I don’t know if it’s a matter of version or something.

I had to do some minor modifications to make it work :

The first part works fine, I can add my stream to the tracklist but the second part, the “play” part does not work. I had to change the code that way :

      "params": {
          "tlid": %s,
        },
        "id": 1
     }""" % json.dumps(trackInfo)

and :
trackInfo = jsonResponse["result"][0]["tlid"]

I wouldn’t make it work without your reply so thank you !

I hope it could help someone else !