8
package blessupboys.speechtest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import android.util.Log;



public class VoiceRecognitionTest extends Activity implements OnClickListener
{

    private TextView mText;
    private SpeechRecognizer sr;
    private static final String TAG = "MyStt3Activity";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_recognition_test);
        Button speakButton = (Button) findViewById(R.id.btn_speak);
        mText = (TextView) findViewById(R.id.textView1);
        speakButton.setOnClickListener(this);
        sr = SpeechRecognizer.createSpeechRecognizer(this);
        sr.setRecognitionListener(new listener());
    }

    class listener implements RecognitionListener
    {
        public void onReadyForSpeech(Bundle params)
        {
            Log.d(TAG, "onReadyForSpeech");
        }
        public void onBeginningOfSpeech()
        {
            Log.d(TAG, "onBeginningOfSpeech");
        }
        public void onRmsChanged(float rmsdB)
        {
            Log.d(TAG, "onRmsChanged");
        }
        public void onBufferReceived(byte[] buffer)
        {
            Log.d(TAG, "onBufferReceived");
        }
        public void onEndOfSpeech()
        {
            Log.d(TAG, "onEndofSpeech");
        }
        public void onError(int error)
        {
            Log.d(TAG,  "error " +  error);
            mText.setText("error " + error);
        }
        public void onResults(Bundle results)
        {
            String str = new String();
            Log.d(TAG, "onResults " + results);
            ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            for (int i = 0; i < data.size(); i++)
            {
                Log.d(TAG, "result " + data.get(i));
                str += data.get(i);
            }
            mText.setText("results: "+String.valueOf(data.size()));
        }
        public void onPartialResults(Bundle partialResults)
        {
            Log.d(TAG, "onPartialResults");
        }
        public void onEvent(int eventType, Bundle params)
        {
            Log.d(TAG, "onEvent " + eventType);
        }
    }
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak)
        {
            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,"voice.recognition.test");

            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
            sr.startListening(intent);
            Log.i("111111","11111111");
        }
    }
}

我的 Android 清单文件看起来像:

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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VoiceRecognitionTest"
            android:label="@string/title_activity_voice_recognition_test"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

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

</manifest>

我的日志显示它由于 onError() 函数中生成的错误 9 而停止。

01-19 20:04:57.776 18480-18480/? I/art: Not late-enabling -Xcheck:jni (already on)
01-19 20:04:57.925 18480-18480/blessupboys.speechtest W/System: ClassLoader referenced unknown path: /data/app/blessupboys.speechtest-2/lib/x86
01-19 20:04:58.341 18480-18515/blessupboys.speechtest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-19 20:04:58.443 18480-18515/blessupboys.speechtest I/OpenGLRenderer: Initialized EGL, version 1.4
01-19 20:04:58.497 18480-18515/blessupboys.speechtest W/EGL_emulation: eglSurfaceAttrib not implemented
01-19 20:04:58.497 18480-18515/blessupboys.speechtest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad79f260, error=EGL_SUCCESS
01-19 20:04:59.033 18480-18480/blessupboys.speechtest I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
01-19 20:10:09.965 18480-18480/blessupboys.speechtest I/111111: 11111111
01-19 20:10:10.049 18480-18480/blessupboys.speechtest D/MyStt3Activity: error 9

我正在尝试运行此示例程序以尝试熟悉语音识别器软件。

这可能是由于我在 android studio 而不是真实设备上运行它而引起的问题吗?我很坚持这一点。

4

5 回答 5

12

如果有人提出这个问题并且其他答案都没有帮助,您可能想尝试使用以下代码请求权限:

private void requestRecordAudioPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String requiredPermission = Manifest.permission.RECORD_AUDIO;

        // If the user previously denied this permission then show a message explaining why
        // this permission is needed
        if (checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_DENIED) {
            requestPermissions(new String[]{requiredPermission}, 101);
        }
    }
}
于 2017-06-07T02:54:09.307 回答
4

ERROR_INSUFFICIENT_PERMISSIONS当 google 没有 record_audio 权限时也会抛出错误 9 。

检查设置 -> 应用程序 -> 谷歌 -> 权限 -> 麦克风

于 2021-05-11T11:28:27.260 回答
1

这只是模拟器不支持它的问题。它适用于物理设备。

于 2016-01-24T22:39:23.873 回答
0

我发现如果你“只是一个物理设备”并且你仍然得到同样的错误,那很可能是因为如果目标 SDK 在 gradle 文件中。你在以后的版本中看到他们已经改变了危险的权限级别,所以通过降级它,你就可以工作了。

“我不是这方面的专家,但这对我很有效”

于 2016-12-12T07:40:22.093 回答
-1

尝试这些链接以获取对您的项目的引用还在这里检查所有 android 清单权限,这是一个示例项目

希望这些提示有助于解决您的问题!

于 2016-01-20T04:51:28.870 回答