语音识别中的果冻豆哔声
Posted
技术标签:
【中文标题】语音识别中的果冻豆哔声【英文标题】:Jelly Bean Beep In Speech Recognition 【发布时间】:2013-05-04 03:43:48 【问题描述】:我一直在寻找不同的线索,但没有找到任何对我遇到的问题有用的东西。我想摆脱语音识别开始时发出的“哔”声。我正在使用 Jelly Bean 4.2.2,所以我不确定早期版本是否存在同样的问题,如果是的话,我也想解决这个问题。另外,我想知道是否有人对如何响应某些语音识别结果有建议。请告诉我如何即兴编写我的代码以包含这些功能。
package com.example.speech;
import java.util.ArrayList;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.util.Log;
public class MainActivity extends Activity implements OnClickListener
ListView lv;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
boolean reseter = false;
private AudioManager mAudioManager;
private volatile boolean mNoSpeechCountDownOn;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
Log.d("Speech", "available = " + available);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mAudioManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);
commandA();
private CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
@Override
public void onTick(long millisUntilFinished)
@SuppressWarnings("synthetic-access")
@Override
public void onFinish()
mNoSpeechCountDownOn = false;
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
;
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
private class SpeechListener implements RecognitionListener
@Override
public void onBeginningOfSpeech()
if (mNoSpeechCountDownOn)
mNoSpeechCountDownOn = false;
mNoSpeechCountDown.cancel();
Log.d("Speech", "onBeginningOfSpeech");
@Override
public void onBufferReceived(byte[] buffer)
Log.d("Speech", "onBufferReceived");
@Override
public void onEndOfSpeech()
Log.d("Speech", "onEndOfSpeech");
@Override
public void onError(int error)
if (mNoSpeechCountDownOn)
mNoSpeechCountDownOn = false;
mNoSpeechCountDown.cancel();
Log.d("Speech", "onError");
@Override
public void onEvent(int eventType, Bundle params)
Log.d("Speech", "onEvent");
@Override
public void onPartialResults(Bundle partialResults)
Log.d("Speech", "onPartialResults");
@Override
public void onReadyForSpeech(Bundle params)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mNoSpeechCountDownOn = true;
mNoSpeechCountDown.start();
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
Log.d("Speech", "onReadyForSpeech");
try
Thread.sleep(4900);
catch (InterruptedException e)
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.d("speech", "Mute on");
@Override
public void onResults(Bundle results)
Log.d("Speech", "results");
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
// Do whatever you want here
try
Thread.sleep(1);
catch (InterruptedException e)
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.d("speech", "Mute on");
@Override
public void onRmsChanged(float rmsdB)
//Log.d("Speech", "onRmsChanged");
@Override
public void onClick(View v)
// TODO Auto-generated method stub
【问题讨论】:
嗨 Hoan,我用你给我的东西记录了这些结果(我已经更新了我的问题)我收到了多个错误,主要是在运行时。我不确定我是否必须声明一个布尔值。如果您不介意查看它,那就太好了! 几乎唯一的问题是您提供的代码中未定义 mNOSpeechCountDownOn 变量。你建议我把它定义为什么? 我仍然收到一个奇怪的错误 threadid=1: thread exiting with uncaught exception (group=0x41b07348),这会导致致命异常:带有标签 AndroidRuntime 的 main。这可能是什么?解决方法是什么? 我删除了你所有的帖子你为什么要把它破解? 我没有更改代码中的任何内容,它与您的编辑相同。看一下代码,按下按钮的时候就报错了。 【参考方案1】:public class MainActivity extends Activity implements OnClickListener
ListView lv;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
boolean reseter = false;
private AudioManager mAudioManager;
Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lvVoiceReturn);
Button b= (Button)findViewById(R.id.bVoice);
b.setOnClickListener(this);
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
Log.d("Speech", "available = " + available);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
private CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
@Override
public void onTick(long millisUntilFinished)
@SuppressWarnings("synthetic-access")
@Override
public void onFinish()
mNoSpeechCountDownOn = false;
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
;
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public void onClick(View v)
// TODO Auto-generated method stub`
Log.d("speech", "button active");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
private class SpeechListener implements RecognitionListener
@Override
public void onBeginningOfSpeech()
if (mNoSpeechCountDownOn)
mNoSpeechCountDownOn = false;
mNoSpeechCountDown.cancel();
Log.d("Speech", "onBeginningOfSpeech");
@Override
public void onBufferReceived(byte[] buffer)
Log.d("Speech", "onBufferReceived");
@Override
public void onEndOfSpeech()
Log.d("Speech", "onEndOfSpeech");
@Override
public void onError(int error)
if (mNoSpeechCountDownOn)
mNoSpeechCountDownOn = false;
mNoSpeechCountDown.cancel();
Log.d("Speech", "onError");
@Override
public void onEvent(int eventType, Bundle params)
Log.d("Speech", "onEvent");
@Override
public void onPartialResults(Bundle partialResults)
Log.d("Speech", "onPartialResults");
@Override
public void onReadyForSpeech(Bundle params)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mNoSpeechCountDownOn = true;
mNoSpeechCountDown.start();
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
Log.d("Speech", "onReadyForSpeech");
@Override
public void onResults(Bundle results)
Log.d("Speech", "results");
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
// Do whatever you want here
@Override
public void onRmsChanged(float rmsdB)
//Log.d("Speech", "onRmsChanged");
【讨论】:
【参考方案2】:@Hoan Nguyen:我发现在 SpeechRecognizer 开始收听时静音“哔”的唯一方法是使用以下方法:
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
这是一个讨厌的 hack,因为我只是想静音 SpeechRecognizer 播放的那种“系统声音”,但使用 mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true)
的另一种解决方案无效。
【讨论】:
【参考方案3】:尝试使用此代码:
mAudioManager.setStreamMute(AudioManager.VIBRATE_TYPE_NOTIFICATION, true);
【讨论】:
setStreamMute 需要一个流来静音...这不是您传递给该方法的内容...此外,您正在使系统音量流静音,因为 vibrate_type_notification 常量恰好与STREAM_SYSTEM 常量。 将常量更改为 AudioManager.STREAM_SYSTEM 可以解决问题,但 SO 成员拒绝了编辑答案的尝试(根据 SO 政策),而是强制将其作为评论发布。以上是关于语音识别中的果冻豆哔声的主要内容,如果未能解决你的问题,请参考以下文章