gpt4 book ai didi

android - 尽管在 list 中设置了 SpeechRecognizer 的权限不足

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:22 24 4
gpt4 key购买 nike

我只是想创建一个虚拟应用程序,用于在单击按钮时进行语音识别(没有弹出窗口或任何内容)。

我的 Android list 文件:

<?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>

以及包含实际发生情况的 fragment :

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

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

最佳答案

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

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

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

关于android - 尽管在 list 中设置了 SpeechRecognizer 的权限不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com