I need to know if an audio file is currently playing so that a python script can wait before playing a new audio file. I am using OSX. The python script is using the 'os' module to spawn a subshell and play an mp3 file using 'afplay'. This script may be called twice in rapid succession. I am trying to avoid playing multiple files at once. Ideally I'd like to have the script wait for the end of the audio file that is currently playing before playing the next. Any help is greatly appreciated. Thank you in advance
The general solution to making sure only one (of a cooperating set of programs) is doing something at once is a mutex of some sort. In this case, you'd obtain the mutex before the
afplay call, and release it right after
afplay finishes. This will guarantee that you're only running one
afplay at a time.
You have several choices for inter-process mutexes, but the easiest is probably the lock file. I don't personally know Python, but Stack Overflow has “Locking a file in Python” which appears to give plenty of options.