【问题标题】:How to detect if speech to text is available on android?如何检测语音到文本是否在 android 上可用?
【发布时间】:2011-01-24 00:21:13
【问题描述】:

我相信我已经弄清楚如何检测安卓设备是否有麦克风,如下所示:

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 设备是否具有语音转文本功能?还是应该使用上述方法来检测?如果有,如何检测设备是否有麦克风?

感谢任何反馈。

【问题讨论】:

    标签: java android media speech-recognition microphone


    【解决方案1】:

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

    // 检查是否存在识别活动 包管理器 pm = getPackageManager(); 列出活动 = pm.queryIntentActivities( 新意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0); 如果 (activities.size() != 0) { speakButton.setOnClickListener(this); } 别的 { speakButton.setEnabled(false); speakButton.setText("识别器不存在"); }

    要测试麦克风是否存在,只需按照 [2] 中的代码和 [3] 中的文档,在调用 prepare() 时,如果麦克风不可用,您应该会收到 IOException:

    记录器 = 新媒体记录器(); 录音机.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 录音机.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 记录器.setOutputFile(路径); 记录器.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

    【讨论】:

    • 我真的需要准备录音吗?我只是想看看有没有麦克风,我根本不想在这个阶段录音。没有 getMicrophoneDevices() 方法之类的吗?并确认,如果音频识别可用,这相当于语音到文本可用吗?此外,除了设备中没有实现麦克风之外,IOException 可能有多种原因。
    • @Tom 是的,我的意思是用于音频识别的语音到文本;请注意,您只是询问底层系统是否存在可以处理 ACTION-RECOGNIZE_SPEECH 意图的活动,因此这并不能保证麦克风可用。
    • @Tom edit: 看这里stackoverflow.com/questions/4607743/… 似乎只有在麦克风可用时才返回识别活动(它可能有 或 RECORD_AUDIO 权限暗示)
    • @Guido,所以为了检测是否有麦克风,我必须准备录音?这对我来说似乎缺少 API...
    • 这种方法的另一个问题是我没有设置路径,因为我不想保存任何录音。你确定这是唯一的方法吗?
    【解决方案2】:

    在阅读了guido的回答后,这就是我想出的。这对我来说看起来很hackish,希望有更好的方法。我会接受guido的回答,但如果有更好的方法,请告诉我。

    package;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.pm.ResolveInfo;
    import android.media.MediaRecorder;
    import android.speech.RecognizerIntent;
    
    public class MediaUtil {
        //returns whether a microphone exists
        public boolean getMicrophoneExists(Context context) {        
                PackageManager packageManager = context.getPackageManager();
        return packageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
        }
    
        //returns whether the microphone is available
        public static boolean getMicrophoneAvailable(Context context) {
            MediaRecorder recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            recorder.setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
            boolean available = true;
            try { 
                recorder.prepare();
            }
            catch (IOException exception) {
                available = false;
            }
            recorder.release();
            return available;
        }
    
        //returns whether text to speech is available
        public static boolean getTTSAvailable(Context context) {
            PackageManager packageManager = context.getPackageManager();
            Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0);
            if (speechActivities.size() != 0) return true;
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2017-01-10
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2018-12-31
      • 2014-01-01
      • 2020-09-06
      • 2016-01-01
      相关资源
      最近更新 更多