How to make playsound not cut the end of mp3 file?

627 Views Asked by At

When I use playsound library in Python and when I try to play the audio in mp3 format, my audio just a few moments before the end just stops. How can I fix this problem?

from playsound import playsound

word = input(">")
while 0<1:
    if(word == "hello"):
        playsound('greetings.mp3')  
1

There are 1 best solutions below

1
On

I've run into the same issue a couple of times recently when using the playsound module on Windows 10. I found that, when playing shorter (around 3 seconds) audio clips, playsound cut the end of it despite the original audio being complete. I noticed two interesting aspects:

  1. For the same audio clip, the time when audio is cut differs from one run to the next.
  2. I couldn't reproduce this when playing longer audios, it only occured with audios up to 5 seconds or so. (Although to be honest, I didn't try an awful lot of options.)

One workaround I figured out seems to work perfectly to me: I added a simple time.sleep(1) right after playsound.playsound() like this:

import playsound 
import os 
import time 

DIR_NAME = "path/to/dir/where/you/store/audio/files/that/you/want/to/play"

playsound.playsound(os.path.join(DIR_NAME,'myaudio_001.mp3'))
time.sleep(1)

Note that depending on the length of your audios, you might want to increase the waiting time from 1 second to a bit more.