问题
I did coding as below. however I want to know whether there is some ways to control the recording duration. I actually, want to have a program which has a start & finish buttons so that I can control to record. I know that it is like an elementary question. but I really need to solve it. help me~ how should I compensate this problem?
import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone()
show = input("enter text: ")
print("Read text\a")
with mic as source:
audio = r.listen(source)
print("recorded\a")
print('Result: ', r.recognize_google(audio, language='ko-KR'))
回答1:
As per recognizer_instance.listen doc, it's a blocking call (i.e. the program doesn't continue until it's done) and the only way to stop the recording is to not talk for recognizer_instance.pause_threshold
(0.8s by default).
To be able to do anything else while recording is active, you need to use recognizer_instance.listen_in_background. It still uses the same signal recognition logic but keeps recording phrases in a loop until you tell it to stop. This implies that the signal recognition logic is supposed to be reliable enough for all practical purposes. If it fails in your case, you probably need to adjust initial energy_threshold.
FWIW, you can record manually with raw pyaudio, then use the resulting file or raw data to construct AudioData.
来源:https://stackoverflow.com/questions/56200907/can-i-control-the-start-finish-time-when-i-use-speech-recognition-in-python