Android 语音识别权限不足(错误代码 9)
Android Speech Recognition Insufficient Permission (Error Code 9)
我正在尝试在没有标准对话的情况下实现语音识别(它在对话中工作正常)。
我一尝试开始收听就收到错误代码 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:
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){}
}
如有任何见解,我们将不胜感激。
On Android 6 此权限是危险权限之一,这意味着您需要要求用户确认(实际获取)。检查 this and this 了解更多详情。
添加到 Sam 的 :当您在 Android 6 上开发应用程序时,系统可能不会提示您批准 "dangerous" 录制音频(麦克风)权限,因此您需要在“设置”中手动打开应用程序并授予权限。
虽然答案就在那里,但你必须把它从很多部分拼凑起来。什么对我有用(我遇到了同样的错误):
- 将权限添加到您的清单中:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
- 根据我的 logcat,我强烈感觉到还需要以下权限:
<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,貌似很多人都是从web,这里明确写出了包名:
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);
}
});
}
在我的例子中,错误消息是“9/权限不足”
解决方法是将 Microphone 权限授予 Google app
参考:https://github.com/react-native-voice/voice/issues/253#issuecomment-812726040
我正在尝试在没有标准对话的情况下实现语音识别(它在对话中工作正常)。
我一尝试开始收听就收到错误代码 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:
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){}
}
如有任何见解,我们将不胜感激。
On Android 6 此权限是危险权限之一,这意味着您需要要求用户确认(实际获取)。检查 this and this 了解更多详情。
添加到 Sam 的
虽然答案就在那里,但你必须把它从很多部分拼凑起来。什么对我有用(我遇到了同样的错误):
- 将权限添加到您的清单中:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
- 根据我的 logcat,我强烈感觉到还需要以下权限:
<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,貌似很多人都是从web,这里明确写出了包名:
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); } }); }
在我的例子中,错误消息是“9/权限不足”
解决方法是将 Microphone 权限授予 Google app
参考:https://github.com/react-native-voice/voice/issues/253#issuecomment-812726040