android应用百度语音识别、语音合成和语音唤醒

该博客介绍了如何在Android应用中集成百度的语音识别、语音合成和语音唤醒功能。首先,开发者需要从百度官网申请并下载SDK。语音识别可以通过API或SDK集成,唤醒词可以自定义。语音合成的SDK提供了详细文档,需要将SDK中的assets和jniLibs文件复制到项目中。博主分享了MainActivity.java、bdspeech_digital_layout.xml和activity_main.xml的部分代码,并提供了完整代码的GitHub链接。
摘要由CSDN通过智能技术生成

android应用百度语音识别、语音合成和语音唤醒


  • 首先申请百度语音识别:http://yuyin.baidu.com/
  • 下载SDK

  • 有两种方式实现语音识别:API和SDK集成

  • 唤醒功能默认唤醒词是百度一下或者小度你好,可以到官网去自定义唤醒词
  • 语音合成下载的SDK里有详细的文档说明,按照里面的做就行了
  • 记得把SDK里面demo的assets和jniLibs复制到项目中,具体看github

  • 直接上代码吧

MainActivity.java

package com.example.ruanyulin.asr_tts;

import android.annotation.TargetApi;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.TelecomManager;
import android.util.AndroidRuntimeException;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
//import com.example.administrator.baiduvoicetest.R;
import com.example.ruanyulin.asr_tts.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
   

    private TextView textResult;
    private EventManager eventManager;
    private String mSampleDirPath; //本地路径
    private String app;
    private SpeechSynthesizer speechSynthesizer;
    private FloatingActionButton floatingActionButton;
    private FloatingActionButton fab_setting;

    //packageName
    private String weixinp = "com.tencent.mm";
    private String qqp = "com.tencent.mobileqq";
    private String zhihup = "com.zhihu.android";
    private String baidumapp = "com.baidu.BaiduMap";
    private String tiebap = "com.baidu.tieba";
    private String zhibo = "air.tv.douyu.android";
    private String train = "com.MobileTicket";
    private String transfor = "com.youdao.dict";
    private String taobao = "com.taobao.taobao";
    private String weibo = "com.sina.weibo";
    private String jingdong = "com.jingdong.app.mall";
    private String tianmao = "com.tmall.wireless";
    private String calendar = "com.android.calendar";
    private String clock = "com.android.alarmclock";
    private String calcu = "com.android.calculator2";


    private static final String TTS_TEXT_MODEL_FILE = "bd_etts_text.dat";
    private static final String TTS_SPEECH_MODEL_FILE = "bd_etts_speech_female.dat";
    private static final String TTS_SPEECH_MODEL1_FILE = "bd_etts_speech_male.dat";
    private static final String TTS_LICENSE_FILE = "temp_license";
    private static final String TTS_ENGLISH_SPEECH_MODEL1 = "bd_etts_speech_female_en.dat";
    private static final String TTS_ENGLISH_SPEECH_MODEL2 = "bd_etts_speech_male_en.dat";
    private static final String TTS_ENGLISH_TEXT_MODEL_FILE = "bd_etts_text_en.dat";
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //textResult = (TextView) findViewById();
        textResult = (TextView) findViewById(R.id.texx);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        fab_setting = (FloatingActionButton) findViewById(R.id.fab_setting);
        textResult.setText("说出指令可进行相关操作 \n" +
                "如 “QQ” 或者 “打开QQ” 则会打开手机QQ\n" +
                "“安装 xx” 则会搜索相关应用\n" +
                "请点击按钮或说出唤醒词启动语音助手\n" +
                "唤醒词为 “合工你好”");

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.baidu.action.RECOGNIZE_SPEECH");
                //设置离线授权文件
                intent.putExtra("grammar","assets:///baidu_speech_grammar.bsg");
                startActivityForResult(intent,1);
            }
        });

        //setting
        fab_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        initEnv();
        initTTS();
        speak("欢迎使用语音助手,请点击按钮或说出唤醒词启动语音识别");

    }

    @Override
    protected void onStart() {
        super.onStart();

        //创建唤醒事件管理器
        eventManager = EventManagerFactory.create(MainActivity.this,"wp");
        //注册唤醒事件监听器
        eventManager.registerListener(new EventListener() {
            @Override
            public void onEvent(String name, String params, byte[] bytes, int i, int i1) {
                try{
                    JSONObject jsonObject = new JSONObject(params);
                    if ("wp.data".equals(name)){
                        Toast.makeText(MainActivity.this,"唤醒成功",Toast.LENGTH_SHORT).show();

                        speak("唤醒成功");
                        TimerTask task = new TimerTask() {
                            @Override
                            public void run() {
                                Intent intent = new Intent("com.baidu.action.RECOGNIZE_SPEECH");
                                //设置离线授权文件
                                intent.putExtra("grammar","asset:///baidu_speech_grammar.bsg");
                                startActivityForResult(intent,1);
                            }
                        };
                        Timer timer = new Timer();
                        timer.schedule(task,1500);


                    }else if ("wp.exit".equals(name)){
                        //Toast.makeText(MainActivity.this,"停止唤醒功能" ,Toast.LENGTH_SHORT).show();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        HashMap params = new HashMap();
        params.put("kws-file","assets:///WakeUp.bin");
        eventManager.send("wp.start",new JSONObject(params).toString(),null,0,0);
        //Toast.makeText(this,"启动唤醒",Toast.LENGTH_SHORT).show();

    }
    @Override protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        //停止唤醒功能
        eventManager.send("wp.stop", null, null, 0, 0);
        //Toast.makeText(this,&
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值