百度语音android框架,Android百度语音识别源码

百度语音搜索,基于强大的语音识别能力,支持通过语音命令快速发起搜索,让搜索更快捷、更智能。对比手机端的文本键盘输入,百度语音搜索是更自然的,符合移动设备的交互方式。在百度强大的人工智能技术支持下,语音搜索前景广阔。语音等现代搜索引擎要做的是连接人与信息及服务,它不仅会倾听,更懂得理解与思考,未来将更好地造福人类。既然这么强大,我们就要好好的利用。

dc0aa8ebe2b114ecb3bc68bddf7fbd99.png

首先要去百度申请开发APP,然后填入appid,key,secret到你程序。有官方源码。这里我只给出最简单的使用代码

package com.voice.bdv;

import org.json.JSONObject;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.os.Bundle;

import android.speech.RecognitionListener;

import android.speech.SpeechRecognizer;

import android.view.View;

import android.widget.Button;

import android.widget.ScrollView;

import android.widget.TextView;

import com.baidu.speech.VoiceRecognitionService;

public class MainActivity extends Activity implements RecognitionListener {

TextView textView;

Button btn;

SpeechRecognizer speechRecognizer;

private static final int EVENT_ERROR = 11;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.btn);

textView = (TextView) findViewById(R.id.log);

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this,

new ComponentName(this, VoiceRecognitionService.class));

speechRecognizer.setRecognitionListener(this);

}

public void onClick(View view) {

start();

}

@Override

protected void onDestroy() {

speechRecognizer.destroy();

super.onDestroy();

}

private void start() {

textView.setText("");

print("点击了“开始”");

Intent intent = new Intent();

intent.putExtra("args", "");

speechRecognizer.startListening(intent);

}

@Override

public void onReadyForSpeech(Bundle params) {

// TODO Auto-generated method stub

print("准备就绪,可以开始说话");

}

@Override

public void onBeginningOfSpeech() {

// TODO Auto-generated method stub

btn.setText("说完了");

//print("检测到用户的已经开始说话");

}

@Override

public void onRmsChanged(float rmsdB) {

// TODO Auto-generated method stub

}

@Override

public void onBufferReceived(byte[] buffer) {

// TODO Auto-generated method stub

}

@Override

public void onEndOfSpeech() {

// TODO Auto-generated method stub

//print("检测到用户的已经停止说话");

btn.setText("识别中");

}

@Override

public void onError(int error) {

// TODO Auto-generated method stub

}

@Override

public void onResults(Bundle results) {

// TODO Auto-generated method stub

String json_res = results.getString("origin_result");

try {

print("origin_result=\n" + new JSONObject(json_res).toString(3));

} catch (Exception e) {

print("origin_result=[warning: bad json]\n" + json_res);

}

btn.setText("开始");

}

@Override

public void onPartialResults(Bundle partialResults) {

// TODO Auto-generated method stub

}

@Override

public void onEvent(int eventType, Bundle params) {

// TODO Auto-generated method stub

switch (eventType) {

case EVENT_ERROR:

String reason = params.get("reason") + "";

print("EVENT_ERROR, " + reason);

break;

case VoiceRecognitionService.EVENT_ENGINE_SWITCH:

// int type = params.getInt("engine_type");

// print("*引擎切换至" + (type == 0 ? "在线" : "离线"));

break;

}

}

private void print(String msg) {

textView.append(msg + "\n");

ScrollView sv = (ScrollView) textView.getParent();

sv.smoothScrollTo(0, 1000000);

}

}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值