【发布时间】:2014-04-16 14:00:09
【问题描述】:
我正在尝试在语音识别期间自定义“对话”。
如果我理解正确,我需要使用SpeechRecognizer 自定义上图中的语音识别 GUI。
这个How to get audio amplitude with speech recognizer? 与我的问题类似,但他询问使用onRmsChanged 添加幅度指示器,因为他已经想出了如何在识别发生时实现新的GUI,所以他的问题虽然有用,比我所在的位置更远一点。
是否有任何现有的示例项目,可以解释这种自定义 UI 是如何实现的。我查看了 ApiDemo VoiceRecognition 示例,但我仍然看不到在哪里设置/更改 UI..
从开发文档中,我了解到这需要在主 UI 线程上。 所以我的伪方法是创建一个 SpeechDialogClass,一个扩展 Dialog 并实现 RecognitionListener 的对话框类。像这样的东西。 我会想象在方法中的某个地方我会设置上下文,onRmsChanged 处理等。但从那里我几乎迷失了。
public class SpeechDialogClass extends Dialog implements RecognitionListener {
public Activity c;
public Dialog d;
public ImageView mic, mic_amp;
public SpeechDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.speech_dialog_kids);
mic = (ImageView) findViewById(R.id.mic_icon);
mic_amp = (ImageView) findViewById(R.id.speech_amplitude);
// //So I would set some sort of listener to change the selector state
// of mic_icon and the
// /somewhere I would set the mic_amp to listen/ract to on onRmsChanged
// public void onRmsChanged(float arg0)///
// // and this is where Im lost///
}
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
setContentView(R.layout.speech_dialog_kids);
}
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
}
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
public void onError(int arg0) {
// TODO Auto-generated method stub
}
public void onEvent(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
}
public void onPartialResults(Bundle arg0) {
// TODO Auto-generated method stub
}
public void onReadyForSpeech(Bundle arg0) {
// TODO Auto-generated method stub
}
public void onResults(Bundle arg0) {
// TODO Auto-generated method stub
}
public void onRmsChanged(float arg0) {
// TODO Auto-generated method stub
// pseudo code//
// mic_amp.doSomething(and a float);
}
}
我的speech_dialiog_kids.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Speak Text"
android:textColor="@android:color/white"
android:textSize="15dp"
android:textStyle="bold" >
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<ImageView
android:id="@+id/speech_amplitude"
android:layout_width="78dp"
android:layout_height="78dp"
android:layout_marginTop="10dp"
android:src="@drawable/amplitude_icon"
android:visibility="visible" />
<ImageView
android:id="@+id/mic_icon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_marginLeft="-73dp"
android:layout_marginTop="16dp"
android:src="@drawable/small_right_grey_white"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
如何调用自定义意图而不是默认意图?
标签: android user-interface speech-recognition