简体   繁体   English

如何同时播放音乐和使用语音命令?

[英]How do I play music and use speak command work at the same time?

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.我正在尝试使用Python创建一个语音助手程序,并希望我的程序在后台播放音乐并同时祝愿我。 But when I'm trying to do so, the program first executes the playsound.playsound() and then executes the wishme command.但是当我尝试这样做时,程序首先执行playsound.playsound()然后执行wishme命令。 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)

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阅读有关线程模块的更多信息,以根据您的需要对其进行自定义

Use: os.startfile('C:\\Users\\socia\\Downloads\\jack_sparrow_bgm.mp3') .使用: 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.它更先进,我目前也在为自己制作相同的程序并使用操作系统模块并且没有遇到任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM