0

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()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

3 Answers3

1

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()
0

by using pygame and pyttsx3

from pygame import mixer
import pyttsx3
engine = pyttsx3.init()
  
say = 'getting details of current voice'
voices = engine.getProperty('voices')      
# engine.setProperty('volume',1.0)    
engine.setProperty('voice', voices[1].id)  
engine.setProperty('rate', 200)     # setting up new voice rate
outfile = "temp.wav"
engine.save_to_file(say, outfile)
engine.runAndWait()
  
mixer.init()
mixer.music.load("temp.wav")
mixer.music.play()


def stop():
    mixer.music.stop()

def pause():
    mixer.music.pause()

def unpause():
    mixer.music.unpause()

 
while True:
      
    print("Press 'p' to pause, 'r' to resume")
    print("Press 'e' to exit the program")
    query = input("  ")
      
    if query == 'p':
        pause() 
    elif query == 'r':
        unpause() 
  
    elif query == 'e':
        mixer.music.stop()
        break

-1

You can use keyboard module of python

if keyboard.is_pressed("q"): #If Q key is pressed
    engine.stop()

Make sure to add import keyboard at the top of your code.

Just for fun
  • 4,102
  • 1
  • 5
  • 11
  • Thanks for the help, but unfortunately i tried that and it doesn't work:/ It must be because while the runAndWait() function is working it doesn't read the code after it. (just guessing) Or im just still making something wrong – gozdekurtulmus Aug 09 '20 at 15:23