Playing audio in pydub

Amen picture Amen · Oct 15, 2014 · Viewed 14.8k times · Source

How can I play a wav audio after importing it to my code?

from pydub import AudioSegment  
song = AudioSegment.from_wav("explosion.wav")

Answer

Jiaaro picture Jiaaro · Oct 15, 2014

If you're just trying to get a quick idea of what your code is doing (in the REPL for instance), you can use pydub.playback:

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_wav("explosion.wav")
play(song)

If you have pyaudio installed, that will be used; it's sometimes tricky to install. Otherwise ffplay will be used.

ffplay is not part of the standard ffmpeg install on all platforms, so take a look at "Getting ffmpeg set up" in the pydub docs if you're going that route.

Another caveat: ffplay is going to cause a window to be opened while the sound is playing, it's almost definitely not an acceptable solution for use in production code. If you want to play audio in production code you'll want to look at other options.