我正在尝试自定义“对话框”语音识别期间的GUI界面。
如果我理解正确,需要使用SpeechRecognizer
来自定义上图中的语音识别GUI界面。
这个问题与我的类似:如何通过语音识别获取音频振幅? 他询问如何使用 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_dialog_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>