Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing audio in pydub

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

from pydub import AudioSegment  
song = AudioSegment.from_wav("explosion.wav")
like image 407
Amen Avatar asked Oct 15 '14 16:10

Amen


1 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.

like image 50
Jiaaro Avatar answered Oct 09 '22 05:10

Jiaaro