Playing an audio file, but not waiting for it to finish?

Steve D'Aprano steve+python at pearwood.info
Thu Sep 15 14:38:43 EDT 2016


On Fri, 16 Sep 2016 04:06 am, kerbingamer376 wrote:

> Hi,
> I have a library that allows me to play sound files. However, the play
> function waits for the sound to finish before it returns, and I'd like to
> be able to start the sound playing, and then have it return immediately so
> my program can continue, but also be able to know when the sound finishes.
> Is this possible, without modifying the library?


Call the library function from a separate thread. See the threading module
for details.

You may have to be careful that no more than one thread ever calls the
library, since it is unlikely to be thread-safe. If it is thread-safe, it
would probably already offer an option to play in the background. And
performance may suffer: depending on how the library is written, you may
find that sound stutters or pauses as control passes back and forth between
your main thread and the thread playing the sound. I guess you'll have to
try it and see.

If threading doesn't work for you, try the multiprocessing library instead.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list