1

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')  
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • If the sound is being cut off, it's because that's the way it is in the .mp3 file. – martineau Sep 10 '20 at 22:01
  • That library is implemented differently on different platforms, so including which OS you use might be helpful. Without knowing which system you're on we can only give general advice, but one thought is that MP3s have gaps at the beginning and end, which could potentially cause issues when playing the audio back if they're not accounted for. Have you checked lots of different MP3s from lots of different sources, including checking them in another audio player to make sure there's no issues? This is all info you should include in your original question. – Random Davis Sep 10 '20 at 22:02
  • @RandomDavis well when I play the original mp3 there is no cut at the end, but when I play it on wings personal platform in my python code it just cuts the last few moments of the mp3 audio. Is there any possible way to prevent mp3 file to being cut? – Grantas Šapkinas Sep 12 '20 at 17:30
  • Again, we can't help you without knowing which OS/platform you're on – Random Davis Sep 14 '20 at 15:56

1 Answers1

0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
lazarea
  • 1,129
  • 14
  • 43
  • is there no way in `playsound` to know when it's done, e.g., as in `pygame.mixer.music.get_busy()` ? – Max May 25 '22 at 19:33