Exec binding to play .mp3

Hi,

I am trying to play an mp3 from the rpi where OH also runs.

I have seen that I can run some commands on the rpi terminal like
wget www.website.com/track1.mp3
mpg321 track1.mp3

and this will play the .mp3 track fine.

Does anyone know if i can execute these commands automatically using the Exec binding and then once I have finished playing the track execute a rm track1.mp3 command to delete it from the directory where i downloaded it.

Many Thanks

hi,
why don’t you put theese commands into a (shell/python) script and execute the script via exec binding?
greets

Hi,

The names of the mp3 tracks can change and I want to create a custom execution command to get a track from a website and play it locally from the pi.

The track names are passed to the rule via mqtt so i need to execute what comes in from MQTT

I thought about using something like mpd but i didn’t want to build another rpi.

you can give your script item states inside of your rule, something like:

executeCommandLine(“python script” + Item.state)

Hi,

That would be great, i will have to research how do to this as I have never use python before.

Would you happen to know of any good resources online I could refer to?

Thanks

maybe someone else as i am a beginner, too.
But i think in your case you don’t need the exec binding if you are already inside of a rule - executeCommandLine should be fine.

Thank you

Yes, I recommend using executeCommandLine inside a rule rather than using the Exec binding. However, you don’t really need to write an external script for this. Just run several executeCommandLines.

var String results = executeCommandLine("wget " + url + mp3, 5000)
logInfo("Exec", results)

results = executeCommandLine("mpg321 " + mp3, 20000) // make timeout longer than longest mp3 you would ever play
logInfo("Exec", results)

results = executeCommandLine("rm " + mp3, 5000)
logInfo("Exec", results)

Where url and mp3 are obtained from somewhere.

1 Like

Great thank you so much this is exactly what i need as my current solution was to have a separate xbmc image on a separate rpi and then issue commands from there to play a .mp4 file online. At least now I can download the next (preemptive) track and play it from the same rpi where OH is running. PERFECT.

@rlkoshak Is it possible to do a wget command while the track is playing from mpg321 /path/track1.mp3?

I don’t know. Your can try and see. It all depend on whether the player needs slip the file before it can start playing.

Of course, I don’t think your executeCommandLine will return until the full file is downloaded.

@rlkoshak I have found a way around this all.

I thought i would share here so others can benefit from this solution.

I create a node.js program (that will run on the same rpi as OH) that will listen to a mqtt channel for a track Url. The program will the play the track using the player plugin (https://www.npmjs.com/package/player)

The OH rule will be the ‘controller’ publishing track url’s and commands (i.e. play and pause) to the mqtt channel.
The Node.js program will be subscribed to the mqtt channel and will respond accordingly.

Since MQTT is quick there should be no delay in the node.js responding asynchronously. There will no need to delete the track as it is streamed via the player plugin for node.js.

I hope this helps the community.