0

Can I remove an audio file when it stopped with playsound module? I have written a code but I can't do it:

from gtts import gTTS
import os
import playsound

def say(textg):
    tts = gTTS(text=textg, lang='en')
    tts.save('audio.mp3')
    playsound('audio.mp3')
    time.sleep(here, what i have to do?)
    os.remove('audio.mp3')

I am using Python 2.7.15

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • It doesn't look like `playsound` has a function to do this. have you tried `playsound(None)`? You could always create an empty or silent audio file and play that to "stop" the sound. – Kevin Welch Jan 11 '19 at 10:30
  • Did any other module have the functionality to do this? Any suggestions! –  Jan 11 '19 at 10:37
  • `Pygame` has the ability to start and stop audio. Take a some time to learn how Pygame works then take a look at https://www.pygame.org/docs/ref/mixer.html – Kevin Welch Jan 12 '19 at 09:01
  • Does this answer your question? [How to stop audio with playsound module?](https://stackoverflow.com/questions/57158779/how-to-stop-audio-with-playsound-module) – Tomerikoo Oct 04 '21 at 16:30

1 Answers1

0

playsound is the name of module, you should use playsound.playsound(which is a function name) instead:

from gtts import gTTS
import os
import playsound

def say(textg):
    tts=gTTS(text=textg,lang='en')
    tts.save('audio.mp3')
    playsound.playsound('audio.mp3')
    os.remove('audio.mp3')

After calling playsound.playsound, the program will wait until it finishes.

keineahnung2345
  • 2,635
  • 4
  • 13
  • 28