Play audio with Python

Play audio with Python

Your best bet is probably to use pygame/SDL. Its an external library, but it has great support across platforms.

pygame.mixer.init()
pygame.mixer.music.load(file.mp3)
pygame.mixer.music.play()

You can find more specific documentation about the audio mixer support in the pygame.mixer.music documentation

Try playsound which is a Pure Python, cross platform, single function module with no dependencies for playing sounds.

Install via pip:

$ pip install playsound

Once youve installed, you can use it like this:

from playsound import playsound
playsound(/path/to/a/sound/file/you/want/to/play.mp3)

Play audio with Python

Take a look at Simpleaudio, which is a relatively recent and lightweight library for this purpose:

> pip install simpleaudio

Then:

import simpleaudio as sa

wave_obj = sa.WaveObject.from_wave_file(path/to/file.wav)
play_obj = wave_obj.play()
play_obj.wait_done()

Make sure to use uncompressed 16 bit PCM files.

Leave a Reply

Your email address will not be published. Required fields are marked *