对于SpeechRecognizer的权限不足,尽管在清单

时间:2016-05-21 02:03:01

标签: android

我只是想在点击一个按钮(没有弹出窗口或任何东西)时创建一个用于语音识别的虚拟应用程序。

我的Android清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="billobob.org.speechtest">
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

</manifest>

这个片段包含了实际发生的事情:

package billobob.org.speechtest;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Simple app for recognizing speech
 */
public class MainActivityFragment extends Fragment {

    protected static final int RESULT_SPEECH = 1234;

    private TextView mSpeechTextView1;
    private TextView mSpeechTextView2;
    private Button mSpeechButton;
    private String speechString;
    private SpeechRecognizer mSpeechRecognizer;
    private Intent mSpeechRecognizerIntent;
    boolean mIsListening = false;

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        mSpeechTextView1 = (TextView) view.findViewById(R.id.textView1);
        mSpeechTextView2 = (TextView) view.findViewById(R.id.textView2);
        mSpeechButton = (Button) view.findViewById(R.id.speechButton);
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this.getContext());
        mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getActivity().getPackageName());


        SpeechRecognitionListener listener = new SpeechRecognitionListener();
        mSpeechRecognizer.setRecognitionListener(listener);

        mSpeechButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mIsListening)
                {
                    Log.d("UUXX", "clicked");
                    mIsListening = true;
                    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
                }
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        if (mSpeechRecognizer != null)
        {
            mSpeechRecognizer.stopListening();
            mSpeechRecognizer.cancel();
            mSpeechRecognizer.destroy();
        }
        super.onDestroyView();
    }

    protected class SpeechRecognitionListener implements RecognitionListener {
        @Override
        public void onReadyForSpeech(Bundle params) {
            Log.d("UUSP", "in read");
        }

        @Override
        public void onBeginningOfSpeech() {
            Log.d("UUSP", "begin!");
        }

        @Override
        public void onRmsChanged(float rmsdB) {

        }

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

        }

        @Override
        public void onEndOfSpeech() {
            Log.d("UUSP", "end");
        }

        @Override
        public void onError(int error) {

        }

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            Log.d("UUSP", matches != null ? matches.get(0) : null);
            mIsListening = false;
            mSpeechTextView1.setText(matches.get(0));
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            Log.d("UUSP", "partial...");
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            Log.d("UUSP", "event?");
        }
    }
}

按钮注册了点击,但没有其他任何事情发生。我在非应用程序日志中注意到错误:

05-20 20:56:32.022 18200-19108/? E/RecognitionService: call for recognition service without RECORD_AUDIO permissions

总是发生,尽管事实上我表面上已经在清单中设置了权限。我正在使用Android Studio 2.1在6P上进行测试。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:6)

对于API 23+(Android 6.0),仅仅向清单添加权限是不够的。您需要在运行时请求权限。

有关详细信息,请参阅开发人员文档: https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous https://developer.android.com/training/permissions/requesting.html

您可以通过将gradle targetSdkVersion更改回21来验证这是问题。然后,它会在运行API 23的设备上使用旧的权限模型

答案 1 :(得分:0)

在您要访问音频记录功能的瞬间添加此代码段。

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);

这是要获得运行时权限才能访问录制音频的功能。您可以在该数组中添加任意数量的权限。

在清单文件中添加它:

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