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来定义自己的语音识别服务,但这超出了此答案的范围:)
- 3 回答
- 0 关注
- 457 浏览
添加回答
举报