标签: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 one rror(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上进行测试.任何帮助将非常感激!
解决方法:
对于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的设备上使用旧的权限模型
标签:android 来源: https://codeday.me/bug/20190527/1166762.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。