Add "stop play after X seconds" feature
Brought to you by:
sobukus
I'm currently writing an app that plays audio at a set time (to eventually replace the bells at the school I work at). Our existing system turns on a relay for X seconds, then stops. I'd like a similar setup for mpg123 (which I shell out to, because of node.js' lacklustre audio support).
Would this be a possibility? I'm sure I could create a minute-long audio file then use setTimeout to kill the process, but having this all in-built would be a lot more reliable.
Are you looping the ringtone? You do need different lengths of ringing?
I see two options with existing mpg123:
Have a short ringtone file (1 s) that loops smoothly, then use
mpg123 --loop n ring.mp3
to have a ringtone for n seconds. Thisshould work nicely since I added gapless decoding.
Have a long ringtone file and only play the first s frames of it via
mpg123 -n s ring.mp3
. You need to work out the number of frames, butfor normal MP3, it's 1152 samples per frame. Together with the sampling
rate, you got your frames per second.
Are you missing an option besides these two?
I did consider --loop, but in the past teachers have also ask that we play music, or something that isn't a loopable ring, so unless I manually trim the audio file to be X seconds, looping won't work.
However -n might work. I'd have to work out the sampling rate and number of frames though.
A third thought just occurred to me: Using -R and issuing a pause / stop command after X seconds, though I think that gets a bit tricky when trying to automate the whole process for reliable playback
Am Mon, 12 Sep 2016 08:26:55 +0000
schrieb "David Gray":
For a usual 44100 Hz file, one MP3 frame is 0.026 s.
Nah, you have to do external timing and still have delay depending on
audio and pipe buffers. It's untidy. Determining the proper length of
audio to play is the thing.