在离线模式下Android上的语音到文本

ami*_*ser 2 android speech-recognition

无论如何,我可以在离线模式下使用Android的Voice to Text功能.

在给定的示例VoiceRecognition.java中,它使用目标RecognizerIntent.ACTION_RECOGNIZE_SPEECH启动和活动.

这是否意味着需要先安装任何其他apk才能使用此功能,或者我是否需要编写自己的应用程序来启动此意图.

我一直在寻找这个,但是很困惑......

这是我用过的代码..

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);
}
Run Code Online (Sandbox Code Playgroud)

在运行此代码时,它会使识别器不存在,这意味着不存在此类活动.怎么解决这个?

Mic*_*evy 6

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

正如Dante Jiang在将语音转换为文本时所说,根据这篇文章,谷歌语音搜索是你真正需要的.

Android SDK可以轻松地将语音输入直接集成到您自己的应用程序中 - 只需从此示例应用程序进行复制和粘贴即可开始使用.Android是一个开放平台,因此您的应用程序可能会在已注册接收RecognizerIntent的设备上使用任何语音识别服务.Google的语音搜索应用程序预先安装在许多Android设备上,通过显示"立即说话"对话框和流式传输音频到Google的服务器(用户点击搜索小部件上的麦克风按钮时使用的相同服务器)来响应识别器内容.启用语音的键盘.(您可以检查设置➝应用程序➝管理应用程序中是否安装了语音搜索.)

在代码中,您应该检查识别活动是否存在.我有以下我用过的片段:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

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