Android使用百度语音识别的示例代码
程序员文章站
2023-12-16 23:44:46
本文使用百度语音识别,完成语音识别的功能,使用百度语音识别,先要申请app id,这个直接到百度网站上有说明文档,本文不再赘述。申请之后,下载sdk包,按照百度官网要求,合...
本文使用百度语音识别,完成语音识别的功能,使用百度语音识别,先要申请app id,这个直接到百度网站上有说明文档,本文不再赘述。申请之后,下载sdk包,按照百度官网要求,合并libs和res两个目录到项目中,然后在build.gradle(module:app)中的android{...}下添加
sourcesets{ main{ jnilibs.srcdirs=['libs'] } }
这样, 百度语音识别的so文件才能正常使用。
manifest文件中添加权限
<uses-permission android:name="android.permission.record_audio" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.change_wifi_state" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.write_external_storage" />
然后还要在manifest中添加
<!-- 请填写应用实际的app_id --> <meta-data android:name="com.baidu.speech.app_id" android:value="app id"/> <!-- 请填写应用实际的api_key --> <meta-data android:name="com.baidu.speech.api_key" android:value="api_key"/> <!-- 请填写应用实际的secret_key --> <meta-data android:name="com.baidu.speech.secret_key" android:value="secret_key"/> <service android:name="com.baidu.speech.voicerecognitionservice" android:exported="false" />
其中的app id,api_key和secret_key替换为你申请的内容。
我们封装了一个工具类,用来使用语音识别
package com.yjp.speechrecognizer; import android.content.componentname; import android.content.context; import android.content.intent; import android.os.bundle; import android.speech.recognitionlistener; import android.speech.speechrecognizer; import android.widget.toast; import com.baidu.speech.voicerecognitionservice; public class speechrecognizertool implements recognitionlistener { public interface resultscallback { void onresults(string result); } private context mcontext; private speechrecognizer mspeechrecognizer; private resultscallback mresultscallback; public speechrecognizertool(context context) { mcontext = context; } public synchronized void createtool() { if (null == mspeechrecognizer) { // 创建识别器 mspeechrecognizer = speechrecognizer.createspeechrecognizer(mcontext, new componentname(mcontext, voicerecognitionservice.class)); // 注册监听器 mspeechrecognizer.setrecognitionlistener(this); } } public synchronized void destroytool() { mspeechrecognizer.stoplistening(); mspeechrecognizer.destroy(); mspeechrecognizer = null; } // 开始识别 public void startasr(resultscallback callback) { mresultscallback = callback; intent intent = new intent(); bindparams(intent); mspeechrecognizer.startlistening(intent); } //停止识别 public void stopasr() { mspeechrecognizer.stoplistening(); } private void bindparams(intent intent) { // 设置识别参数 } @override public void onreadyforspeech(bundle params) { // 准备就绪 toast.maketext(mcontext, "请开始说话", toast.length_short).show(); } @override public void onbeginningofspeech() { // 开始说话处理 } @override public void onrmschanged(float rmsdb) { // 音量变化处理 } @override public void onbufferreceived(byte[] buffer) { // 录音数据传出处理 } @override public void onendofspeech() { // 说话结束处理 } @override public void onerror(int error) { } @override public void onresults(bundle results) { // 最终结果处理 if (mresultscallback != null) { string text = results.get(speechrecognizer.results_recognition) .tostring().replace("]", "").replace("[", ""); mresultscallback.onresults(text); } } @override public void onpartialresults(bundle partialresults) { // 临时结果处理 } @override public void onevent(int eventtype, bundle params) { } }
mainactivity的界面如下
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" android:gravity="center" tools:context="com.yjp.speechrecognizer.mainactivity"> <button android:id="@+id/startspeechbutton" android:layout_width="60dp" android:layout_height="40dp" android:background="@drawable/bdspeech_btn_orangelight_normal" android:text="按住说话"/> <textview android:id="@+id/speechtextview" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
mainactivity的类实现为:
package com.yjp.speechrecognizer; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.motionevent; import android.view.view; import android.widget.button; import android.widget.textview; public class mainactivity extends appcompatactivity implements speechrecognizertool.resultscallback { private button mstartspeechbutton; private textview mtextview; private speechrecognizertool mspeechrecognizertool = new speechrecognizertool(this); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtextview = (textview) findviewbyid(r.id.speechtextview); mstartspeechbutton = (button) findviewbyid(r.id.startspeechbutton); mstartspeechbutton.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { int action = event.getaction(); switch (action) { case motionevent.action_down: mspeechrecognizertool.startasr(mainactivity.this); mstartspeechbutton.setbackgroundresource( r.drawable.bdspeech_btn_orangelight_pressed); break; case motionevent.action_up: mspeechrecognizertool.stopasr(); mstartspeechbutton.setbackgroundresource( r.drawable.bdspeech_btn_orangelight_normal); break; default: return false; } return true; } }); } @override protected void onstart() { super.onstart(); mspeechrecognizertool.createtool(); } @override protected void onstop() { super.onstop(); mspeechrecognizertool.destroytool(); } @override public void onresults(string result) { final string finalresult = result; mainactivity.this.runonuithread(new runnable() { @override public void run() { mtextview.settext(finalresult); } }); } }
可以运行看一下效果,感觉识别率还是不错的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。