首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >语音重组助手类

语音重组助手类
EN

Code Review用户
提问于 2015-12-02 21:42:10
回答 1查看 50关注 0票数 3

我正在做一个演讲重组项目。我有一个util类,它由三种方法组成:检查麦克风是否存在、检查麦克风的可用性和检查文本到语音的可用性。有人能检查我的密码吗?

代码语言:javascript
复制
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;
    }
EN

回答 1

Code Review用户

回答已采纳

发布于 2015-12-02 22:35:29

  • 返回布尔型如果(条件)返回true的模式;要避免返回false。返回条件;以更透明的方式实现同样的目标。在这种情况下,返回speechActivities.size() != 0;
  • 测试和收购考虑到了这种情况:当你调用getMicrophoneAvailable时,它可能是可用的,但后来当你的应用程序真正需要它时,它已经被其他应用程序成功地声称了。这意味着没有必要对可用性进行测试。根据需要获取它:公共静态MediaRecorder getMicrophone(上下文上下文){.尝试{ recorder.prepare();} catch (IOException异常){返回null;}返回记录器;}
票数 5
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/112668

复制
相关文章

相似问题

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