-1

I am trying to create a voice assistant program using Python and want my program to play music in the background and wishme at the same time. But when I'm trying to do so, the program first executes the playsound.playsound() and then executes the wishme command. What should I do?

if __name__ == '__main__':
    playsound.playsound('C:\\Users\\socia\\Downloads\\jack_sparrow_bgm.mp3')
    wishMe()
    while True:

      query = takeCommand().lower()

      if 'wikipedia' in query:
          speak('Searching Wikipedia...')
          query = query.replace("wikipedia", "").replace("on", "").replace("search", "")
          results = wikipedia.summary(query, sentences=2)
          speak('Here is your result SIR.')
          print(results)
          speak(results)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

2 Answers2

0

Use the threading module. This will make your tasks independent of each other. Example,

import threading

def task_1():
    #do something like play music

def task_2():
    #do something like search wiki

#Run the code
threading.Thread(target=task_1).start()
threading.Thread(target=task_2).start()

Read more on the threading module for customising it as per your need

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Primus
  • 36
  • 5
  • Probably don't want to create the threads and start them in an infinite loop like that… – martineau Aug 07 '21 at 07:19
  • Edited to remove the loop, which doesn't make any sense. It's possible, but certainly not guaranteed that the *worker functions* might want to loop infinitely. – Karl Knechtel Aug 07 '21 at 07:30
0

Use : os.startfile('C:\\Users\\socia\\Downloads\\jack_sparrow_bgm.mp3').

It is more advance and I am also making the same program for myself currently and using OS module and faced no problem.

AaYan Yasin
  • 566
  • 3
  • 15
  • but this OS module redirects me to the default music player to play the music... And I want the program to play music in the background as well as speak the commands for some specific period of time (while starting)... – Ashutosh Singh Aug 08 '21 at 11:46