[Tutor] playing sound files in python

Steven D'Aprano steve at pearwood.info
Fri Dec 7 05:49:58 EST 2018


On Fri, Dec 07, 2018 at 02:17:55AM +0000, nathan tech wrote:
> Hello all!
> 
> My name is nate, and I am relatively new to this list, relatively being 
> just signed up.
> 
> I have a question that you would think would be obvious, but alas I have 
> struggled to figure out.
> 
> How do I play sound files in python.

Alas, its not obvious nor easy.

https://duckduckgo.com/?q=python+play+sound+files

 
> More specificly, I want to play ogg files especially, with wav and mp3 
> also being a high priority.

I believe that wxPython supports .wav files, but not .ogg or .mp3.

Playing sound in Python is a definite weakness unless you have a 
third-party library like PyGame that supports it in a platform- 
independent way.

You could try:

    print('\a')

but this requires that you are running in a terminal that supports the 
BEL character, that the system beep has not been turned off, and even if 
it works, its only a short beep.

If your OS (Windows?) has a sound file player, you could try 
calling out to it with os.system. Under Linux, I might try 
something like this:

os.system('mpg123 -q why_is_the_rum_gone-remix.mp3')

but that pauses until paying is over, and requires the mpg123 player. 
There are other players, like ogg123. What you get on Windows, I don't 
know.

Pygame is starting to sound more attractive :-)



-- 
Steve


More information about the Tutor mailing list