java在安卓 studio中使用SpeechRecognizer时,哪个函数返回录制的音频?
我正在开发一个定制的键盘应用程序(在安卓 studio中),其中添加了一个麦克风键。我想把录制的语音转换成文本,所以我使用了语音识别器。但我还要录音。我希望我的键盘应用程序在whatsapp等聊天应用程序中录制音频时,应该将音频作为消息发送。有人能帮我在使用语音识别器时如何获取录制的音频吗。我只需要使用语音识别器,因为我需要它用于其他目的。 (注:我的应用程序没有任何活动) 这是记录语音并转换成文本的代码
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.example.keyboard");
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this.getApplicationContext());
RecognitionListener listener = new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
Toast.makeText(EDMTKeyboard.this, "Recording Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onBeginningOfSpeech() {
Toast.makeText(EDMTKeyboard.this, "Recording Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
System.err.println("Error listening for speech: " + error);
Toast.makeText(EDMTKeyboard.this, "ERROR-"+error, Toast.LENGTH_SHORT).show();
}
@Override
public void onResults(Bundle results) {
Bundle bundle = intent.getExtras();
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (voiceResults == null) {
System.out.println("No voice results");
} else {
System.out.println("Printing matches: ");
for (String match : voiceResults) {
Toast.makeText(EDMTKeyboard.this, match, Toast.LENGTH_SHORT).show();
ic.commitText(match,match.length());
str = str + match;
I=I+match.length()+1;
//System.out.println(match);
}
}
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);
我在stackoverflow中检查了类似的问题,但给出的答案目前不起作用(可能是安卓 studio删除了这些功能)
共 (0) 个答案