Playing audio in pydub

How can I play the audio wav after importing it into my code?

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

      

+3


source to share


1 answer


If you're just trying to quickly understand what your code is doing (in the REPL for example), 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 this will be used; sometimes difficult to install . Otherwise ffplay will be used.



ffplay

is not included in the default ffmpeg installation on all platforms, so take a look at " Getting ffmpeg setup " in the pydub docs if you go that route.

One more caveat: ffplay will force the window to open while audio is playing, which is almost certainly not an acceptable solution for use in production code. If you want to play audio in production code, you need other options.

+4


source







All Articles