如何检测语音到文本是否在Android上可用? [英] How to detect if speech to text is available on android?

查看:105
本文介绍了如何检测语音到文本是否在Android上可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我已经弄清楚了如何检测android设备是否具有麦克风,例如:

I believe I have figured out how to detect if an android device has a microphone, like so:

Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0);
        TextView micAvailView = (TextView) findViewById(R.id.mic_available_flag);
        if (speechActivities.size() != 0) { //we have a microphone

        }
        else { //we do not have a microphones

        }

但是,如何检测android设备是否具有语音转文本功能?还是应该使用上述方法来检测?如果是这样,如何检测设备是否有麦克风?

However, how does one detect whether the android device has speech-to-text capability? Or should the above be used to detect that? If so, how does one detect if the device has a microphone?

感谢您提供任何反馈意见.

Any feedback is appreciated, thanks.

推荐答案

您随附的代码确实用于检测音频识别是否可用[1]:

The code you attached is indeed used to detect if audio recognition is available [1]:


// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List 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");
}

要测试是否存在麦克风,只需遵循[2]中的代码和[3]中的文档,在调用prepare()时,如果麦克风不可用,则应获得IOException:

To test if a mic is present, just follow the code in [2] and the docs at [3], when calling prepare() you should get an IOException if the mic is not available:


recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();

[1] http://developer.android.com/resources/articles /speech-input.html
[2] http://developer.android.com/guide/topics/media/index.html#capture
[3] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html

[1] http://developer.android.com/resources/articles/speech-input.html
[2] http://developer.android.com/guide/topics/media/index.html#capture
[3] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html

这篇关于如何检测语音到文本是否在Android上可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆