为什么SpeechRecognizer说我的权限不足,即使我已经指定了RECORD_AUDIO权限?

时间:2015-03-10 05:57:17

标签: android speech-recognition android-permissions

我试图在我的应用中获得语音识别功能。我收到权限错误,说我需要RECORD_AUDIO权限,但我已在我的清单中指定了该权限。我在Android L上,也为该API版本构建(没有更低)。这是确切的错误:

03-09 22:43:58.996    1555-1576/com.google.android.googlequicksearchbox:search E/RecognitionService﹕ call for recognition service without RECORD_AUDIO permissions

什么了?这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andrewstromme.myapp" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.INTERNET" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
来自MainActivity.java的

public class MainActivity extends Activity implements RecognitionListener {
private SpeechRecognizer mRecognizer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
    mRecognizer.setRecognitionListener(this);
}

public void listenForSpeech() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
            .getPackage().getName());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "say something");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    mRecognizer.startListening(intent);
}

void showToastMessage(String message){
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech() {
    showToastMessage("start of speech");

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {

}

@Override
public void onError(int error) {
    if (error == SpeechRecognizer.ERROR_AUDIO) {
        showToastMessage("audio recording error");
    } else if (error == SpeechRecognizer.ERROR_CLIENT) {
        showToastMessage("client side error");
    } else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
        showToastMessage("insufficient permissions");
    } else if (error == SpeechRecognizer.ERROR_NETWORK) {
        showToastMessage("network error");
    } else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
        showToastMessage("network timeout");
    } else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
        showToastMessage("no speech match");
    } else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
        showToastMessage("recognizer busy");
    } else if (error == SpeechRecognizer.ERROR_SERVER) {
        showToastMessage("server error");
    } else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
        showToastMessage("no speech input");
    } else {
        showToastMessage("unknown error");
    }
}

@Override
public void onResults(Bundle results) {
    String result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0);
    TextView t = (TextView)findViewById(R.id.spokenText);
    t.setText(result);
}

@Override
public void onPartialResults(Bundle partialResults) {

}

@Override
public void onEvent(int eventType, Bundle params) {

}
}

1 个答案:

答案 0 :(得分:1)

哦,我觉得很傻。我的<uses-permission ...>代码必须位于我的<application></application>代码之外。