首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测android上是否有语音转文本功能?

如何检测android上是否有语音转文本功能?
EN

Stack Overflow用户
提问于 2011-01-23 05:48:55
回答 1查看 3K关注 0票数 3

我相信我已经知道了如何检测android设备是否有麦克风,如下所示:

代码语言:javascript
复制
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设备是否具有语音转文本功能?还是应该使用上面的内容来检测这一点?如果有,如何检测设备是否有麦克风?

任何反馈都很感谢,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2011-01-23 22:00:24

在读完guido的答案后,这是我想到的。对我来说,这看起来很黑客,希望有更好的方法。我会接受guido的回答,但如果有更好的方法,请告诉我。

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4770835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档