I'm trying to make an assistant but when the user wants to play a song, it keeps playing it until finishes. I want it to stop when the user presses a key. It is same for the engine.say()
, too.
I couldn't find a way to interrupt actions on their docs. There is engine.stop()
for pyttsx3 but I couldn't make it work. I thought it might be because of the engine.runAndWait()
but if I don't include it the machine says nothing. How can I solve these problems?
I can try using another module too if there is a way to solve this.
import pyttsx3
from playsound import playsound
if "play" in input:
songName = input[5:] + ".mp3"
try:
playsound(songName)
except:
engine.say("I couldn't find the song.")
engine.runAndWait()
I solved the problem by using pygame
module. It has everything we would want to use as a feature. If someone else is having a problem like that you can try it.
import pygame
def playSong(songName):
pygame.mixer.music.load(songName)
pygame.mixer.music.play()
if "play" in input:
try:
songName = input[5:]+".mp3" #Takes the song name user wanted
speak("That's a nice song choice.")
playSong(songName)
except:
speak("I couldn't find the song.")
Also I tried to make the song stop and continue but it didn't actually work. But i leave it here as an idea
if ("stop") and ("song" or "music") in input:
pygame.mixer.music.pause()
if ("resume" or "continue") and ("song" or "music") in input:
pygame.mixer.music.unpause()