首页 > 解决方案 > 从另一个函数调用时音频未完成

问题描述

我有以下代码准工作

from gtts import gTTS
import speech_recognition as rs
import pyaudio
import audioop
import os
import math
from os import system
import threading
from datetime import datetime

def say(text):

    filename = 'speak.mp3'
    language = 'en'      
    myobj = gTTS(text=text, lang=language, slow=False)  
    myobj.save(filename) 
    player = vlc.MediaPlayer(filename)
    player.play()
               
def listen(x):
    r=rs.Recognizer()
    with rs.Microphone() as source:
        audio=r.listen(source)
    try:
        text = r.recognize_google(audio)
        process(text.lower())
    except:
            system('say I did not get that. Please say again.')
            listen(0)

def process(text):
    print(text)
    # Do things here based on text
    if 'what time is it' in text:
        say(datetime.now().strftime("%H%M"))
    return

#process("what time is it") "Seventeen oh six"
# Listen for me to say something, if I say 'what time is it' return the time
#listen(0)                  "Se"

如果我手动运行进程(文本),例如:

process("what time is it")

Python 会回复我类似“1706”(17 哦六)但是,如果我从 listen() 函数调用它,python 将开始播放文件,但它会被切断,更像是“Se”,然后什么都没有.

我尝试了多种方法,包括使用 time.sleep(n),但是当通过 listen(n) 函数调用时,似乎没有任何更改可以使整个文件播放。

标签: pythonpython-3.xmedia-playervlc

解决方案


推荐阅读