美文网首页
百度语音搜索

百度语音搜索

作者: jiluyixia | 来源:发表于2019-11-25 16:15 被阅读0次

语音搜索。
先实现搜索界面。


sh.jpg

定义输入框:search_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_edittext"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/searchEd"
            android:drawableLeft="@drawable/search_gray"
            android:background="@null"
            android:layout_width="270dp"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/speak"
            android:src="@drawable/audio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <TextView
        android:text="搜索"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_marginTop="23dp"
        android:paddingLeft="5dp"
        android:textSize="17dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

这个搜索框可以手动输入,也可以点击语音图标,用百度语音识别结果搜索。

speak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(context,SpeakPageActivity.class),1);
                //fragment界面执行startActivityForResult,前面不要加context,加了context
                //onActivityResult会不执行,应该是执行到context那个onActivityResult里面了
            }
});

这里专门创建了一个SpeakPageActivity来处理语音。


bd.jpg

speak_page.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="15dp"
    android:text="你可以这样说\n\n\n故宫门票\n北京一日游\n迪士尼乐园"
    android:layout_height="wrap_content"/>

    <FrameLayout
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
       >
        <ImageView
            android:id="@+id/audio_button"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:background="@drawable/shape_circle" />
        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center"
            android:background="@drawable/audio_white" />

    </FrameLayout>

    <ImageView
        android:id="@+id/audio_cancel"
        android:background="@drawable/audio_cancel"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="50dp"
        android:layout_marginRight="50dp"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

点击audioButton,执行动画:

private void setAnim() {
        AnimationSet as = new AnimationSet(true);
        //缩放动画,以中心从原始放大到0.5倍
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        //渐变动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        as.setDuration(1000);
        as.addAnimation(scaleAnimation);
        as.addAnimation(alphaAnimation);
        as.setRepeatMode(Animation.REVERSE);//重复反向执行
        audioButton.startAnimation(as);
}

并且开始录音:

Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
asrManager.start(params);

当说话停止,或者手指松开,都会停止录音并识别:

else if(event.getAction() == MotionEvent.ACTION_UP){
       asrManager.stop();
       audioButton.clearAnimation();//关掉动画。
}

识别到结果后,传到SearchPageActivity界面。

public void onAsrFinalResult(String[] results, RecogResult recogResult) {
            audioButton.clearAnimation();
            String message = results[0];
            Log.v("aaaa","--"+message);
            if(message != null) {
                Intent intent = new Intent();
                intent.putExtra("result", message);
                setResult(3, intent);
            }
            SpeakPageActivity.this.finish();
}

销毁语音界面,并执行百度语音的release()方法。

protected void onDestroy() {
        asrManager.release();
        super.onDestroy();
}

SearchPageActivity接受数据。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.v("nn","nn");
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null) {
            String str = data.getStringExtra("result");
            searchEd.setText(str);
        }
}

代码如下:
https://github.com/doudousang/baidusearch.git
参考代码:
一看就懂的 startActivityForResult
谈谈Fragment中的onActivityResult
关于Android Activity之间传递数据的6种方式
RelativeLayout的layout_marginBottom属性失效问题

相关文章

  • 百度语音搜索

    语音搜索。先实现搜索界面。 定义输入框:search_page.xml 这个搜索框可以手动输入,也可以点击语音图标...

  • “语义搜索”是什么,对SEO有什么影响?

    近日,SEO专家都在讨论语音搜索,随着百度手机客户端,语音搜索的上线,“语义搜索”越来越被大家开始重视,大家一致认...

  • Flutter携程APP页面实现

    SeacherBar 搜索页面 目录 SeacherBar 语音界面 百度SDK接入 对InputDecorati...

  • Day 1 -笔记

    1.百度搜索的手机端和pc端有识图功能,不同于pc的是,移动端除了百度首页外,其他搜索二级页没有语音和图片搜索功能...

  • 语音识别

    语音识别技术已经相当成熟,并且商业化的模式也广为接受 网上搜索市面语音技术业界比较优秀的几家厂商进行对比 百度语音...

  • 语音搜索

  • BAT和JD在AI上的布局

    百度在人工智能方面,声势浩大在北京、硅谷两地建立了人工智能实验室,围绕搜索、地图、金融业务,展开语音搜索搜索、图像...

  • 智能语音客服服务助手

    智能语音客服服务助手 语音识别 阿里语音识别 百度语音识别 讯飞语音识别 语音合成 阿里语音合成 百度语音合成 讯...

  • 如何利用如下4个策略,做语音搜索优化

    如果你手机中安装过百度APP,你会发现偶尔使用语音搜索的时候非常方便,久而久之,你就会觉得在移动端搜索的时候,如果...

  • 科大讯飞cordova插件填坑及api介绍

    项目要求语音合成,准备调用科大讯飞的cordova插件,百度仅有一篇有价值的参考文章(再吐槽下,百度搜索到各种复制...

网友评论

      本文标题:百度语音搜索

      本文链接:https://www.haomeiwen.com/subject/mvkjwctx.html