我试图在Android 4.4中创建一个连续的语音识别,简单地在TextView中显示语音,就像口述一样。我遵循了多个教程,如https://github.com/fcrisciani/android-speech-recognition/blob/master/VoiceRecognition/src/com/speech/fcrisciani/voicerecognition/ContinuousDictationFragment.java,或者有一种方法来使用语音识别器API直接语音输入?并实现了以下版本:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class VoiceReadActivity extends Activity {
private ImageButton mButtonSpeech;
private TextView mTextView;
private SpeechRecognizer mSpeechRecognizer = null;
private SpeechRecognizer getSpeechRecognizer() {
if (mSpeechRecognizer == null) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
mButtonSpeech.getBackground().setColorFilter( 0xFFFF0000, PorterDuff.Mode.MULTIPLY );
}
@Override
public void onBeginningOfSpeech() {
mTextView.append( "BEGINNING TO HEAR..." );
mButtonSpeech.getBackground().setColorFilter( 0xFFFFFFFF, PorterDuff.Mode.MULTIPLY );
}
@Override
public void onBufferReceived(byte[] buffer) {}
@Override
public void onEndOfSpeech() {
mTextView.append( "STOP HEARING..." );
mButtonSpeech.getBackground().setColorFilter( 0xFF00FFFF, PorterDuff.Mode.DST );
}
@Override
public void onError(int error) {
String message;
boolean restart = true;
switch (error)
{
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
restart = false;
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
restart = false;
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Not recognised";
break;
}
mTextView.append("onError code:" + error + " message: " + mes sage);
if (restart) {
getSpeechRecognizer().cancel();
startVoiceRead();
}
}
@Override
public void onEvent(int eventType, Bundle params) {}
@Override
public void onPartialResults(Bundle partialResults) {}
@Override
public void onResults(Bundle results) {
ArrayList<String> text = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
mTextView.append(text.get(0));
mTextView.append( ". " );
startVoiceRead();
}
@Override
public void onRmsChanged(float rmsdB) {}
});
}
return mSpeechRecognizer;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSpeech = (ImageButton)findViewById(R.id.button_speech);
mTextView = (TextView)findViewById(R.id.text_slave);
mButtonSpeech.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mSpeechRecognizer == null) {
startVoiceRead();
} else {
stopVoiceRead();
}
}
});
}
public void startVoiceRead() {
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
getSpeechRecognizer().startListening(speechIntent);
mButtonSpeech.getBackground().setColorFilter( 0xFFFFFF00, PorterDuff.Mode.MULTIPLY );
}
public void stopVoiceRead() {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
}
mButtonSpeech.getBackground().setColorFilter( 0xFFFFFFFF, PorterDuff.Mode.DST);
}
}
我已经添加权限到我的Android Manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
问题是,像onBeginningOfSpeech()这样的RecognitionListener函数平均每三次被随机调用,我想激活它投掷按钮按下。此外,我没有收到任何结果,但ErrorCode 2:网络错误,如果激活了RecognitionListener。即使我遵循了教程,我也无法发现其中的区别,这使得我的代码直到现在都无法正常工作。我的LogCat输出在按下按钮时抛出以下错误:
07-04 10:20:22.714: E/DatabaseUtils(869): Writing exception to parcel
07-04 10:20:22.714: E/DatabaseUtils(869): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-04 10:20:22.714: E/DatabaseUtils(869): at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:14614)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2258)
07-04 10:20:22.714: E/DatabaseUtils(869): at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:663)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.content.ContentProvider$Transport.call(ContentProvider.java:325)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:275)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.os.Binder.execTransact(Binder.java:404)
07-04 10:20:22.714: E/DatabaseUtils(869): at dalvik.system.NativeStart.run(Native Method)
07-04 10:20:22.714: W/ActivityManager(869): Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-04 10:20:22.734: E/VoiceEngineWrapper(16366): getInstance() : get existed VoiceEngine
07-04 10:20:22.734: E/VSG(16366): VSG: speechEndpointTimeout = 1500
07-04 10:20:22.734: E/VSG(16366): VSG: DEFAULT_ENDPOINT_MEDIUM is 1750
07-04 10:20:22.734: E/VSG(16366): VSG: Not using dynamic HANGOVER ... speechEndpointTimeout is 1500
07-04 10:20:22.734: E/VSG(16366): VSG: SHORT = 400
07-04 10:20:22.734: E/VSG(16366): VSG: MEDIUM = 750
07-04 10:20:22.734: E/VSG(16366): VSG: MEDIUM_LONG = 1250
07-04 10:20:22.734: E/VSG(16366): VSG: LONG = 1750
07-04 10:20:22.734: E/VSG(16366): VSG: LONG_LONG = 2250
但是即使我加上
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
上述问题仍在继续。
谢谢你花时间帮助我。这是我一直无法克服的障碍。
根据使用语音识别器产生ERROR_NETWORK(值2),错误是由三星语音API(由Vlingo提供支持)引起的。将手机设置中的语音API更改为标准的Google API解决了问题。
对于可能有此问题的其他人,我遇到了一个语音识别器的NetworkError,因为在同一时间有一个重叠的MediaRecorder或AudioRecord实例活动。它在我正在开发的联想Tab 2 A10上似乎运行良好,但在我测试的任何移动设备上都不行。
搜索SpeechRecognizer和NetworkError无效。
如果你有这个问题,你可能会发现这个问题很有用(或开始搜索语音识别器和AudioRecord/MediaRecorder)…
Android语音识别与音频同步录制
我一直看到网络错误。禁用的三星自动Bixby醒了!问题解决了!