安卓自定义语音识别GUI对话框

8

我正在尝试自定义“对话框”语音识别期间的GUI界面。 enter image description here

如果我理解正确,需要使用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>

enter image description here


你如何调用自定义意图而不是默认的意图? - confusedstudent
1个回答

1
我想在方法中设置上下文和onRmsChanged等处理,但从那里开始我就不知道该怎么做了。

类似这样的:

public void onRmsChanged(float rms) {
    if (rms < limit1)
        mic_amp.setImageResource(1);
    else if (rms < limit2)
        mic_amp.setImageResource(2);
    else if (rms < limit3)
        mic_amp.setImageResource(3);
}

所以它会在rms变化时脉冲。您可以根据rms级别更改图像。您可以更改各种ImageView方法以更改实际图像。
另一个问题是,根据Android版本,onRmsChanged并不总是被调用,因此这使得实现此功能变得更加困难。那么可能最简单的方法是保留原始对话框。

非常感谢您的帮助。我会尝试一下。另一个问题是,根据Android版本,onRmsChanged并不总是被调用。是的,我也读到过这个问题,不知道为什么会这样。再次感谢。 - Mcorv
因为Google不希望你重新打造他们的引擎。 - Nikolay Shmyrev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接