How do i control when to stop the audio input?

淺唱寂寞╮ 提交于 2020-06-02 06:20:46

问题


I am using the SpeechRecognition Python package to get the audio from the user.

import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

This piece of code when executed starts listening for the audio input from the user. If the user does not speak for a while it automatically stops.

  • I want to know how can we get to know that it has stopped listening to audio?
  • How can I manually disable it ? I mean if i want to listen audio for 50 seconds and then stop listening to any further audio?

回答1:


I think you need to read the library specifications; then, you can check that using record method instead of listen method is preferable to your application.




回答2:


  1. as the documentation specifies, recording stops when you exit out of with. you may print something after with to know that the recording has been stopped.
  2. here's how you can stop recording after 50 seconds.
import speech_recognition as sr 
recognizer = sr.Recognizer() 
mic = sr.Microphone(device_index=1) 
with mic as source:
    recognizer.adjust_for_ambient_noise(source)
    captured_audio = recognizer.record(source=mic, duration=50)


来源:https://stackoverflow.com/questions/43720729/how-do-i-control-when-to-stop-the-audio-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!