- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我只是想创建一个虚拟应用程序,用于在单击按钮时进行语音识别(没有弹出窗口或任何内容)。
我的 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/
SpeechRecognizer 正常工作了很长时间,突然它不工作了,现在没有检测到任何东西.. 示例代码是这样的。当它在线时它可以正常工作但离线时不能,我错过了吗什么地方都有??? Spee
我正在尝试在没有 Intent Dialog 的情况下在 Android 上使用 SpeechRecognizer。它在大多数设备上都可以正常工作,但有些设备会返回音频录制错误(错误代码 3)并且没有
我正在编写WinRT应用,并在VS 2015,x86调试中对其进行测试。我正进入(状态 我的应用程序未创建不需要的音频输出。这是我的代码: using Windows.Media.SpeechReco
我在 Activity 中使用 Native SpeechRecognizer 服务,它“有效”,但我遇到了一些严重的问题。 返回的“语音转文本”坦率地说非常令人震惊,而且非常非常差,在应用程序中肯定
简介 我需要在我的代码中实现语音识别。我按照这里的其他帖子和一些教程来获取它,但它不适合我。 方法 这是在 onCreate 中初始化它的代码: Log.d("SPEECH", "speech rec
我遇到一个问题,SpeechRecognizer 将正确收听,当我下次收听它时转到 SpeechRecognizer.ERROR_NO_MATCH,识别器立即无法 SpeechRecognizer.E
我知道 SpeechRecognizer 在 android 中是如何工作的。我有一个要求,我需要在一段时间后调用 SpeechRecognizer.stopListening() 方法。但在那之后,
我正在学习有关 iOS 10 语音识别 API 的一些教程 (https://code.tutsplus.com/tutorials/using-the-speech-recognition-api-
我正在使用 SppechRecognizer用于语音识别器应用程序。它工作正常。我的要求是我想在 1 秒或 2 秒后停止收听语音。如何实现? 最佳答案 1 或 2 秒似乎不是很多时间,但如果你想设置一
有没有办法在通话时运行 SpeechRecognizer?我是这样做的: BroadcastReceiver 处理电话状态的变化(例如摘机)。 SpeechRecognizer 在当前(主)线程中启动
我正在尝试在我的 Android 应用程序中使用 SpeechRecognizer 库,到目前为止,它的工作给我留下了很多问题。首先,当我停止说话时它不会停止。如果我试图停止语音识别自己,下次它会给我
如何找到默认系统语音识别器的 ComponentName,即 createSpeechRecognizer(Context context) 时返回的那个叫做? (其实我只需要知道它支持哪些输入语言,
我正在尝试在 Android 4.4 中创建连续语音识别,简单地在 TextView 中显示口语,就像命令一样。我遵循了多个教程,例如 https://github.com/fcrisciani/an
我在 Android 中使用 SpeechRecognizer 和 RecognizerIntent 来实现语音识别。我的目标是在我的语音识别器在屏幕上显示结果后重新开始收听语音。为此,我使用了以下代
我正在使用名为 SpeechRecognizer 的 Android 语音 API 尝试将语音翻译成文本,但由于某种原因,只要我点击按钮 - 我就会看到消息 “目前无法访问谷歌” ,不等我讲话, wi
我使用 SpeechRecognizer 来实现“语音到文本”功能,她的工作结果是文本数据: Intent intent = new Intent(RecognizerIntent.ACTION_RE
我有一个 maind UI 类,其中有一个按钮实例化一个类,该类正在实现 SpeechRecognizer 库以将语音从文本转换为文本。 btnSpeak = (ImageButton) findVi
我有一个相当奇怪的问题。我有一个 Android 应用程序,我使用 SpeechRecognizer 类为其添加语音识别。我创建了实现 RecognitionListener 的类,它只打印每个事件的
我正在开发 UWP 并想使用 SpeechRecognizer。它应该只对“Next”和“Back”这两个词使用react。但通常,它会将“NExt”识别为“Back”。我的代码如下。如何解决? va
请看下面的代码。 onBeginningOfSpeech() 被调用(甚至在我开始说话之前,顺便说一句),但随后 - 没有。我错过了什么? 我承认代码大部分是由不同的例子组成的,我没有完全理解。但我希
我是一名优秀的程序员,十分优秀!