Android 在实现 RecognitionListener 时获得意图
2022-08-31 15:43:33
正如Iftah在回答中所述,我可以通过将Uri从意图传递给以下位置来获得Android中语音Recoginition录制的音频:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// the recording url is in getData:
Uri audioUri = data.getData();
}
在这里,正是我想要的,没有问题。Intent data
一切都很完美,但是该解决方案为用户提供了何时说话的提示,我不想这样做,所以为了解决这个问题,我让我的活动实现RefisionListener,如下所示:
public class MainActivity extends AppCompatActivity implements RecognitionListener {
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "sv_SE");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "sv_SE");
recognizerIntent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
recognizerIntent.putExtra("android.speech.extra.GET_AUDIO", true);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
speech.startListening(recognizerIntent);
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("Debug", "On ready for speech");
}
@Override
public void onBeginningOfSpeech() {
Log.d("Debug", "On beggining of speech");
}
@Override
public void onRmsChanged(float rmsdB) {
Log.d("Debug", "Rsm changed");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("Debug", "Buffer recieved");
}
@Override
public void onEndOfSpeech() {
Log.d("Debug", "On end of speech");
}
@Override
public void onError(int error) {
Log.d("Debug", "Error");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("Debug", "On partial result");
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("Debug", "On event");
}
@Override
public void onResults(Bundle results) {
Log.d("Debug", "On result");
}
}
这绕过了提示hovewer,我无法弄清楚如何像第一个示例中那样获取Uri,因为这里:
@Override
public void onResults(Bundle results) {
Log.d("Debug", "On result");
// The results bundle don't contain the URI!
}
我得到一个不包含意图或Uri的。我尝试过查看捆绑包中的所有密钥,并且没有URI或意图存在,我也尝试过,但这不会返回任何内容。Bundle results
getIntent()
我感谢任何帮助或朝着正确的方向前进。