android - 在离线模式下在 Android 上语音转文字

标签 android speech-recognition

无论如何,我可以在离线模式下使用 Android 的语音转文本功能。

在给定的示例 VoiceRecognition.java 中,它以 RecognizerIntent.ACTION_RECOGNIZE_SPEECH 为 Intent 启动和 Activity 。

这是否意味着需要事先安装任何其他 apk 才能使其正常工作,或者我是否需要编写自己的应用程序才能启动此 Intent 。

我一直在寻找这个很长一段时间,但越来越困惑......

这是我使用的代码..

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

private ListView mList;

/**
 * Called with the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate our UI from its XML layout description.
    setContentView(R.layout.voice_recognition);

    // Get display items for later interaction
    Button speakButton = (Button) findViewById(R.id.btn_speak);

    mList = (ListView) findViewById(R.id.list);

    // Check to see if a recognition activity is present
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(
            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() != 0) {
        speakButton.setOnClickListener(this);
    } else {
        speakButton.setEnabled(false);
        speakButton.setText("Recognizer not present");
    }
}

/**
 * Handle the click on the start recognition button.
 */
public void onClick(View v) {
    if (v.getId() == R.id.btn_speak) {
        startVoiceRecognitionActivity();
    }
}

/**
 * Fire an intent to start the speech recognition activity.
 */
private void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

/**
 * Handle the results from the recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it could have heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                matches));
    }

    super.onActivityResult(requestCode, resultCode, data);
}

在运行此代码时,它会显示 Recognizer not present,这意味着不存在此类 Activity 。如何解决?

最佳答案

我认为你有两个问题。首先,是的,识别器功能并非在所有设备上都可用。请务必安装并更新最新的 Android 版 Google 语音搜索。我相信它安装了最新的识别器。参见 http://www.google.com/mobile/voice-actions/这可能会有所帮助。

正如Dante Jiang 在Converting speech to text 中所说, 根据 this article , Google Voice Search是您真正需要的。

The Android SDK makes it easy to integrate speech input directly into your own application—just copy and paste from this sample application to get started. Android is an open platform, so your application can potentially make use of any speech recognition service on the device that's registered to receive a RecognizerIntent. Google's Voice Search application, which is pre-installed on many Android devices, responds to a RecognizerIntent by displaying the "Speak now" dialog and streaming audio to Google's servers—the same servers used when a user taps the microphone button on the search widget or the voice-enabled keyboard. (You can check if Voice Search is installed in Settings ➝ Applications ➝ Manage applications.)

在代码中,您应该检查识别 Activity 是否存在。我使用了以下代码 fragment :

// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
        new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) 
{
    speakButton.setOnClickListener(this);
} 
else 
{
    speakButton.setEnabled(false);
    speakButton.setText(R.string.recognizer_not_present);
}

第二个问题是 Android 语音识别需要互联网连接。识别不是在设备上执行的,而是使用 Google 网络服务。所以,你必须在线。有关 Web 服务的一些信息,请访问 http://waxy.org/2008/11/deconstructing_google_mobiles_voice_search_on_the_iphone/。 .

关于android - 在离线模式下在 Android 上语音转文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4734030/

相关文章:

android - 如何使用 Firestore 填充引用字段

c# - 如何确定SpeechRecognitionEngine识别词的位置?

android - Kotlin Android Studio-在类型为Intent的可为空的接收器上仅允许安全(?。)或非空声明(!!。)调用。

android - 为 PreferenceFragment 的 ListPreference 加载内容时不确定 ProgressBar

android - 将数据插入 SQLite 时出现间歇性 NPE

android - 将项目迁移到 gradle 文件??本项目不使用 Gradle 构建系统。我们建议您迁移到使用 Gradle 构建系统

ios - 每 2 分钟记录一次用户语音和清晰的文本文件

node.js - 语音 XML :lang ="en-US" xml:gender ="Male" name="Microsoft Server Speech Text is always in Female voice

audio - 自动搜索并剪切mp3音频文件中的每个单词

javascript - ionic 应用程序无法识别 Android 手机中的语音