我正在尝试在没有标准对话框的情况下实现语音识别(它在对话框中正常工作)。
我在尝试开始收听时收到错误代码9。
我的设备是LG G Stylo(运行Android 6.0)。
清单:
<manifest package="example.com.myapplication"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
.....
(也尝试添加INTERNET权限,即使这不应该是必要的,因为离线识别应该正常工作)
的build.gradle:
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "example.com.appname"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
语音识别代码:
private SpeechRecognizer speechRecognizer;
protected void onCreate(Bundle savedInstanceState) {
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new speech_listener());
Intent intent = new intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
getApplication().getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
speechRecognizer.startListening(intent);
监听器(内部)类:
class speech_listener implements RecognitionListener
{
public void onReadyForSpeech(Bundle params){}
public void onBeginningOfSpeech(){}
public void onRmsChanged(float rmsdB){}
public void onBufferReceived(byte[] buffer){}
public void onEndOfSpeech(){}
public void onError(int error){
Log.d("Speech", "error: " + error);
}
public void onResults(Bundle results)
{
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String answer = (String)data.get(0);
processAnswer(answer);
}
public void onPartialResults(Bundle partialResults){}
public void onEvent(int eventType, Bundle params){}
}
任何见解都将受到赞赏。
答案 0 :(得分:13)
添加到Sam的answer:当您在Android 6上开发应用时,可能不会提示您批准“危险”的录制音频(麦克风)权限,因此您需要手动打开应用在“设置”中并授予权限。
答案 1 :(得分:11)
答案 2 :(得分:0)
虽然答案就在那里,但你必须把它从很多部分拼凑起来。什么对我有用(我得到了同样的错误):
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.BLUETOOTH" />
如果其他一切都失败,请尝试添加在较新版本的android中,您还必须在运行时请求用户的许可。在正确的位置执行此操作至关重要,您无法在代码中的任何位置运行权限请求,必须从UI线程运行。对我来说,以下工作:
void doPermAudio()
{
int MY_PERMISSIONS_RECORD_AUDIO = 1;
MainActivity thisActivity = this;
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.RECORD_AUDIO},
MY_PERMISSIONS_RECORD_AUDIO);
}
}
}
// in main activity's onCreate
protected void onCreate(Bundle savedInstanceState) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
doPermAudio();
}
}
});
}
另外,在其他地方已经提到过,你可能会在启动监听意图时得到错误9来声明一个错误的包名,因为似乎很多人已经从网上的样本中复制了他们的功能,其中包名称是明确写出来的:
void listen()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
String pkg = getApplication().getPackageName();
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, pkg);
sr.startListening(intent);
}
});
}
答案 3 :(得分:0)
就我而言,错误消息是“9/权限不足”
已通过向 Google 应用授予 麦克风 权限
解决参考:https://github.com/react-native-voice/voice/issues/253#issuecomment-812726040