- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在谷歌上找不到答案,或者我没有找到合适的词。
所以 SpeechRecognizer 工作正常。但是当我听到哔哔声(我在没有谷歌对话框的情况下使用它)并且我在大约 3 秒或更长时间内什么也没说时,它就像识别器什么都不做然后消失,没有听到第二声哔哔声,没有 onResult(),没有 EndofSpeech。
那么,当识别器正在聆听而您什么也没说时会发生什么?哪个方法被触发?
编辑:毕竟它有效,非常感谢 OpiateFuchs 和他非常好的评论和回答。我这样编辑简化的代码,你们可以看看如何制作它。
即使你什么也没说, onPartialResult() 也会经常被调用,但当这种情况发生时,partialResult 字符串是空的,所以如果它是空的,你就知道什么也没说。(来自 OpiateFuchs 的想法)
这就是我对识别器很重要的简化代码:
public Constructor (){
speech = SpeechRecognizer.createSpeechRecognizer(context);
speech.setRecognitionListener(this);
recogIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recogIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "de");
speech.startListening();
}
public void startListening(){
speech.startListening(recogIntent);
if(timerRunning==false){
setcdt();
mCountDownTimer.start();
timerRunning=true;
}
}
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
Toast.makeText(c, "work", Toast.LENGTH_SHORT);
//too see if its called
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Toast.makeText(c, matches.get(0), Toast.LENGTH_LONG).show();
speech.cancel();
analyse(matches.get(0));
m.next(); //calls the Recognizer again
}
@Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> matches = partialResults
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(matches.get(0).length() == 0){
m.tv.append("nothing"); //show on textview
}
else{
m.tv.append("cancel timer "); //show on textview
mCountDownTimer.cancel();
hasSpoken = true;
timerRunning = false;
}
}
@Override
public void onEvent(int eventType, Bundle params) {
}
//innerclass
public class FinishSpeechRecognizerTimer extends CountDownTimer {
public FinishSpeechRecognizerTimer(long startTime, long interval){
super(startTime,interval);
}
@Override
public void onFinish(){
timerRunning = false;
if (hasSpoken==false){
m.tv.append("nospeak ");
speech.cancel();
m.tv.append("listen again after no speak ");
startListening();
}
}
@Override
public void onTick(long millisUntilFinish){
}
}
public void setcdt(){
startTime = 5000;
interval = 4000; //want no onTick - interval > startTime
timerRunning = false;
hasSpoken = false;
mCountDownTimer = new FinishSpeechRecognizerTimer(startTime, interval);
}
最佳答案
对于本次讨论,我尝试提供一个从头开始的示例,它将引导您走向正确的方向。正如评论中所说,似乎 SpeechRecognition
如果什么都不说,它不会自己停下来,所以你可以implement
一个CountDownTimer
例如,完成 SpeechRecognizer
一段时间后:
全局化SpeechRecognizer
(就像你所做的那样),boolean
和 CountdownTimer
objects
:
private SpeechRecognizer speech;
private boolean hasSpoken=false;
private CountDownTimer mCountDownTimer;
private long startTime = 30000L;
private long interval = 1000L;
private boolean timerRunning = false;
扩展CountDownTimer class
:
public class FinishSpeechRecognizerTimer extends CountDownTimer{
public FinishSpeechRecognizerTimer(long startTime, long interval){
super(startTime,interval);
}
@Override
public void onFinish(){
if(hasSpoken==false){
speech.cancel();
}
timerRunning=false;
}
@Override
public void onTick(long millisUntilFinish){
//do whatever you want to do
}
}
初始化
speech = SpeechRecognizer.createSpeechRecognizer(yourContext);
mCountDownTimer = new FinishSpeechRecognizerTimer(startTime, interval);
启动它并同时启动CountDownTimer
:
speech.startListening(recogIntent);
if(timerRunning==false){
mCountDownTimer.start();
timerRunning=true;
}
一旦有人说话,设置 boolean value
hasSpoken
至 true
和 cancel
计时器:
@Override
public void onBeginningOfSpeech() {
hasSpoken=true;
mCountDownTimer.cancel();
timerRunning=false;
}
正如我所说,它是从头开始的,不能保证它能正常工作。此示例启动 CountDownTimer
持续 30 秒,并每秒检查是否有人说话。您要等多久取决于您。
事实证明,在某些情况下,onBeginOfSpeech() 方法会在没有任何人讲话的情况下被多次调用。对于所有感兴趣的人:
而不是在 onBeginOfSpeech()
中做那些事情,您可以使用方法 onPartialResult():
@Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULT_RECOGNITION);
if(matches.size()==0){
hasSpoken=false;
}else{
hasSpoken=true;
mCountDownTimer.cancel();
timerRunning=false;
}
}
关于java - 语音识别器 |什么都没说之后发生了什么(什么都没发生)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36453120/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!