Search code examples
python-3.xvlcspeech-synthesis

How to delay the launch of an mp3 file using vlc


from gtts import *
import os
import vlc
def sauvegarde(text,i):
    tts = gTTS(text, lang='fr')
    tts.save("text"+str(i)+".mp3")
def play(file):
    p = vlc.MediaPlayer(file)
    p.play()
text = "Salut Santo c\'est Alexis, ton maitre qui te parle"
text2="Bonjour Santo"
sauvegarde(text,1)
sauvegarde(text2,2)
play("text1.mp3")
play("text2.mp3")

os.system("pause")

When I run this code the two .mp3 files that I created previously are being played at the same time. Any ideas how to play the second one after the first is finished?


Solution

  • vlc doesn't seem to have a blocking version of play as far as I can tell, so probably the easiest way to accomplish this is to manually wait for the first mp3 to finish. So to make a blocking version of your play function, try something like this:

    import time
    def play(file):
        p = vlc.MediaPlayer(file)
        p.play()
        while(p.is_playing()):
            time.sleep(0.1)
    

    This will check is p is still playing every 100ms. Only when it is done playing, the play function will return.

    Alternatively, you can use vlc.MediaListPlayer and vlc.MediaList to create a playlist containing your mp3's.