如何控制何时停止音频输入? [英] How do i control when to stop the audio input?

查看:37
本文介绍了如何控制何时停止音频输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpeechRecognition Python软件包从用户那里获取音频.

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.

  • 我想知道我们如何才能知道它已经停止听音频了?
  • 如何手动禁用它?我的意思是,如果我想听50秒钟的音频,然后停止再听其他音频了?

推荐答案

  1. 如文档所示,当您退出 with 时,录制会停止.您可以在 with 之后打印一些内容,以了解记录已停止.
  2. 这是在50秒后停止录制的方法.
  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)

这篇关于如何控制何时停止音频输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆