0

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

5
  • Are you forking out to afplay(1) or how are the files being played? Or does the code need to be aware if iTunes or the like is active?
    – thrig
    Commented Aug 26, 2016 at 15:46
  • I imported os into my python script and am currently playing audio files through a subshell with afplay
    – Yllier123
    Commented Aug 26, 2016 at 15:50
  • Please edit your question and add more detail. We need to know exactly how the files are being played.
    – terdon
    Commented Aug 26, 2016 at 15:53
  • @terdon edits have been made
    – Yllier123
    Commented Aug 26, 2016 at 16:07
  • A lockfile would be one solution, so that subsequent instances will fail if some other instance has a lock on a file. Python probably has modules for this.
    – thrig
    Commented Aug 26, 2016 at 16:25

1 Answer 1

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .