为了账号安全,请及时绑定邮箱和手机立即绑定

带有录制的声音片段的Android语音识别?

带有录制的声音片段的Android语音识别?

回首忆惘然 2019-12-12 14:10:13
我已经在Android上使用了语音识别功能,而且我喜欢它。这是我的客户最受好评的功能之一。但是,格式有些限制性。您必须调用识别器的意图,让其将录音发送给Google以进行转录,然后等待文本返回。我的一些想法需要在我的应用程序中记录音频,然后将剪辑发送给Google进行转录。有什么方法可以将音频片段与语音转换为文本?
查看完整描述

3 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

据我所知,仍然没有办法直接将音频剪辑发送给Google进行转录。但是,Froyo(API级别8)引入了SpeechRecognizer类,该类提供对语音识别服务的直接访问。因此,例如,您可以开始播放音频剪辑,并让“活动”启动语音识别器在后台监听,这将在完成后将结果返回到用户定义的监听器回调方法。


以下示例代码应在Activity中定义,因为SpeechRecognizer的方法必须在主应用程序线程中运行。另外,您还需要将RECORD_AUDIO权限添加到您的AndroidManifest.xml中。




    boolean available = SpeechRecognizer.isRecognitionAvailable(this);

    if (available) {

        SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);

        sr.setRecognitionListener(new RecognitionListener() {

            @Override

            public void onResults(Bundle results) {

                // process results here

            }

            // define your other overloaded listener methods here

        });

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        // the following appears to be a requirement, but can be a "dummy" value

        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.dummy");

        // define any other intent extras you want


        // start playback of audio clip here


        // this will start the speech recognizer service in the background

        // without starting a separate activity

        sr.startListening(intent);

    }


您还可以通过扩展RecognitionService来定义自己的语音识别服务,但这超出了此答案的范围:)



查看完整回答
反对 回复 2019-12-13
  • 3 回答
  • 0 关注
  • 428 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信