Playing audio in pydub

Question:

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

from pydub import AudioSegment  
song = AudioSegment.from_wav("explosion.wav")
Asked By: Amen

||

Answers:

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.

Answered By: Jiaaro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.