公司简介
发展历程
高端网站建设
微信开发
APP开发
网络营销服务
电商网站定制
生物医药网站建设
外贸网站建设
教育培训网站建设
小程序开发
功能应用
客户案例
网站建设案例
小程序案例
电商平台案例
APP案例
系统平台案例
网站建设
网站设计
网站营销
小程序
公司地址
人才招聘
地址:成都市太升南路288号锦天国际A幢1002号
电话:028-86922220
为企业提供网站建设、域名注册、服务器等服务
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网络空间、营销软件、网站建设、夏河网站维护、网站推广。
用olami sdk语音识别语义理解做在线听书
olamisdk实现了把录音或者文字转化为用户可以理解的json字符串,本文使用olami sdk做了一个在线听书的demo,用的是喜马拉雅的在线听书sdk.基于eclipse开发环境,libs目录下jar和so文件如下:
olami-android-sdk.jar//olami sdk的jar
afinal_0.5.1_bin.jar
litepal.jar
gson-2.2.4.jar
okhttp-2.4.0.jar
okhttp-urlconnection-2.2.0.jar
okio-1.4.0.jar
opensdk.jar //上面这几个都是喜马拉雅需要的jar
libspeex.so //olami sdk需要用到speex压缩功能
libxmediaplayer.so //喜马拉雅so
libxmediaplayer_x.so//喜马拉雅so
概述: VoiceSdkService中定义了OlamiVoiceRecognizer语音识别引擎,通过点击MusicActivity的开始button启动录音,录音结果在VoiceSdkService中的onResult()回调中拿到识别的Json字符串,在processServiceMessage()函数中处理后找到要听书的名称,然后进入BookUtil进行搜索,搜索到结果后通知VoiceSdkService进行播放,并通知MusicActivity更新播放进度等信息。
package="com.olami.musicdemo" android:versionCode="1" android:versionName="1.0" > android:minSdkVersion="8" android:targetSdkVersion="21" /> android:name="com.olami.musicdemo.OlamiApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="app_key" android:value="b617866c20482d133d5de66fceb37da3"/> android:name="pack_id" android:value="com.app.test.android" /> android:name=".MusicActivity" android:label="@string/app_name" > android:name=".VoiceSdkService" android:exported="true" > android:name= "com.ximalaya.ting.android.opensdk.player.service.XmPlayerService" /> 2.layout布局文件layout_musicview.xmlTextView有两个,tv_name显示听书的名称, tv_totoal_time显示听书的总时间。 ProgressBar实时刷新显示听书的进度 xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/transparent"> android:text="name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_centerHorizontal="true" android:id="@+id/tv_name"/> android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:id="@+id/progressbar_music"/> android:text="total_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressbar_music" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:id="@+id/tv_total_time"/> activity_music.xml xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
package="com.olami.musicdemo"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8" android:targetSdkVersion="21" /> android:name="com.olami.musicdemo.OlamiApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="app_key" android:value="b617866c20482d133d5de66fceb37da3"/> android:name="pack_id" android:value="com.app.test.android" /> android:name=".MusicActivity" android:label="@string/app_name" > android:name=".VoiceSdkService" android:exported="true" > android:name= "com.ximalaya.ting.android.opensdk.player.service.XmPlayerService" /> 2.layout布局文件layout_musicview.xmlTextView有两个,tv_name显示听书的名称, tv_totoal_time显示听书的总时间。 ProgressBar实时刷新显示听书的进度 xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/transparent"> android:text="name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_centerHorizontal="true" android:id="@+id/tv_name"/> android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:id="@+id/progressbar_music"/> android:text="total_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressbar_music" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:id="@+id/tv_total_time"/> activity_music.xml xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:minSdkVersion="8"
android:targetSdkVersion="21" />
android:name="com.olami.musicdemo.OlamiApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="app_key" android:value="b617866c20482d133d5de66fceb37da3"/> android:name="pack_id" android:value="com.app.test.android" /> android:name=".MusicActivity" android:label="@string/app_name" > android:name=".VoiceSdkService" android:exported="true" > android:name= "com.ximalaya.ting.android.opensdk.player.service.XmPlayerService" />
android:name="com.olami.musicdemo.OlamiApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="app_key" android:value="b617866c20482d133d5de66fceb37da3"/> android:name="pack_id" android:value="com.app.test.android" /> android:name=".MusicActivity" android:label="@string/app_name" > android:name=".VoiceSdkService" android:exported="true" > android:name= "com.ximalaya.ting.android.opensdk.player.service.XmPlayerService" />
android:name="app_key"
android:value="b617866c20482d133d5de66fceb37da3"/>
android:name="pack_id" android:value="com.app.test.android" /> android:name=".MusicActivity" android:label="@string/app_name" > android:name=".VoiceSdkService" android:exported="true" > android:name= "com.ximalaya.ting.android.opensdk.player.service.XmPlayerService" />
android:name="pack_id"
android:value="com.app.test.android" />
android:name=".MusicActivity" android:label="@string/app_name" >
android:name=".MusicActivity"
android:label="@string/app_name" >
android:name=".VoiceSdkService" android:exported="true" >
android:name=".VoiceSdkService"
android:exported="true" >
android:name= "com.ximalaya.ting.android.opensdk.player.service.XmPlayerService" />
android:name=
"com.ximalaya.ting.android.opensdk.player.service.XmPlayerService"
/>
layout_musicview.xml
TextView有两个,tv_name显示听书的名称, tv_totoal_time显示听书的总时间。 ProgressBar实时刷新显示听书的进度
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/transparent"> android:text="name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_centerHorizontal="true" android:id="@+id/tv_name"/> android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:id="@+id/progressbar_music"/> android:text="total_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressbar_music" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:id="@+id/tv_total_time"/> activity_music.xml xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
android:text="name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_centerHorizontal="true" android:id="@+id/tv_name"/> android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:id="@+id/progressbar_music"/> android:text="total_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressbar_music" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:id="@+id/tv_total_time"/> activity_music.xml xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:text="name"
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:id="@+id/tv_name"/>
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:id="@+id/progressbar_music"/> android:text="total_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressbar_music" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:id="@+id/tv_total_time"/> activity_music.xml xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:layout_below="@+id/tv_name"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="@+id/progressbar_music"/>
android:text="total_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressbar_music" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:id="@+id/tv_total_time"/> activity_music.xml xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:text="total_time"
android:layout_below="@+id/progressbar_music"
android:id="@+id/tv_total_time"/>
activity_music.xml
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.olami.musicdemo.MusicActivity" > android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="com.olami.musicdemo.MusicActivity" >
android:id="@+id/tv_inputText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="输入:" /> android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/tv_inputText"
android:text="输入:" />
android:id="@+id/tv_volume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_inputText" android:layout_below="@+id/tv_inputText" android:layout_marginTop="40dp" android:text="音量:" /> android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/tv_volume"
android:layout_alignLeft="@+id/tv_inputText"
android:layout_below="@+id/tv_inputText"
android:layout_marginTop="40dp"
android:text="音量:" />
android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_volume" android:layout_marginTop="20dp" android:maxLines="15" android:ellipsize="end" android:text="服务器返回sentence:" android:visibility="visible" /> android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" > android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/tv_result"
android:layout_below="@+id/tv_volume"
android:layout_marginTop="20dp"
android:maxLines="15"
android:ellipsize="end"
android:text="服务器返回sentence:"
android:visibility="visible" />
android:id="@+id/music_view" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_centerInParent="true" >
android:id="@+id/music_view"
android:layout_height="80dp"
android:layout_centerInParent="true"
>
android:id="@+id/et_content" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_above="@+id/btn_stop" android:layout_alignLeft="@+id/tv_inputText" android:layout_marginBottom="10dp" android:layout_toLeftOf="@+id/btn_send" android:background="#E7E7E7" android:singleLine="true" android:text="上海的天气" /> android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/et_content"
android:layout_height="40dp"
android:layout_above="@+id/btn_stop"
android:layout_marginBottom="10dp"
android:layout_toLeftOf="@+id/btn_send"
android:background="#E7E7E7"
android:singleLine="true"
android:text="上海的天气" />
android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_content" android:layout_alignBottom="@+id/et_content" android:layout_alignParentRight="true" android:text="提交" /> android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/btn_send"
android:layout_alignBaseline="@+id/et_content"
android:layout_alignBottom="@+id/et_content"
android:layout_alignParentRight="true"
android:text="提交" />
android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="开始" /> android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/btn_start"
android:layout_alignParentBottom="true"
android:text="开始" />
android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_content" android:layout_alignParentBottom="true" android:text="停止" /> android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/btn_stop"
android:layout_alignLeft="@+id/et_content"
android:text="停止" />
android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/et_content" android:text="取消" /> 自定义MusicView比较简单,代码如下:public class MusicView extendsRelativeLayout{ private Context mContext; private Handler mHandler; private TextView mTextViewName; private TextView mTextViewTotalTime; private ProgressBar mProgressBar; public MusicView(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater =(LayoutInflater) context.getSystemService( context.LAYOUT_INFLATER_SERVICE); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.layout_musicview,this,true); mTextViewName = (TextView) view.findViewById(R.id.tv_name); mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time); mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music); } public void initMusicView(Context context,Handler handler) { mContext = context; mHandler = handler; } public void setMusicName(String name) {//设置播放名称 mTextViewName.setText(name); } public void setProgress(int progress) {//设置播放进度 mProgressBar.setProgress(progress); } public void setTotalTime(String time) {//设置播放总时间 mTextViewTotalTime.setText(time); }}布局效果图如下: 3.MusicActivity和VoiceSdkService通信本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口 public interface CommunicationAssist { public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);}然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。 3.1OlamiApplication1)注册MusicActivity到VoiceSdkService的回调public voidsetActivityToServiceListener(CommunicationAssist listener){ ActivityToServiceListener = listener;}这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。 2)注册 VoiceSdkService到MusicActivity回调public voidsetServiceToActivityListener(CommunicationAssist listener){ mServiceToActivityListener = listener;} 这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。 3.2MusicActivitypublic class MusicActivity extends Activity{ private Handler mHandler; private Handler mInComingHandler; private ActivityComAssist mActivityComAssist; private Button mBtnStart; private Button mBtnStop; private Button mBtnCancel; private Button mBtnSend; private EditText mEditText; private TextView mTextView; private TextView mInputTextView; private TextView mTextViewVolume; private BookUtil mBookUtil = null; private MusicView mMusicView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); initHandler();//初始化handler用于内部消息处理 initInComingHandler();//用于处理来自VoiceSdkService的消息 initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以 //通过getServiceToActivityListener()获得回调向MusicActivity发送消息 initView();//初始化view控件 Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); startService(intent);//启动后台服务 } private void initView() { mBtnStart = (Button) findViewById(R.id.btn_start); mBtnStop = (Button) findViewById(R.id.btn_stop); mBtnCancel = (Button) findViewById(R.id.btn_cancel); mBtnSend = (Button) findViewById(R.id.btn_send); mInputTextView = (TextView) findViewById(R.id.tv_inputText); mEditText = (EditText) findViewById(R.id.et_content); mTextView = (TextView) findViewById(R.id.tv_result); mTextViewVolume = (TextView) findViewById(R.id.tv_volume); mBtnStart.setOnClickListener(newOnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); } }); mBtnStop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); mBtnStart.setText("开始"); Log.i("led","MusicActivity mBtnStop onclick 开始"); } }); mBtnCancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null); } }); mBtnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { sendMessageToService( MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText()); mInputTextView.setText("文字:"+mEditText.getText()); } }); mMusicView = (MusicView) findViewById(R.id.music_view); //if(mMusicView != null) //mMusicView.initMusicView(MusicActivity.this,mHandler); } private void initHandler() { mHandler = new Handler(){ @Override public voidhandleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: break; default: break; } } }; } //InComingHandler 收到来自VoiceSdkService的消息用于更新界面, //包括开始录音,结束录音,播放的书的名称和进度,音量等信息。 private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: mBtnStart.setText("录音中"); Log.i("led","MusicActivity 录音中"); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: mBtnStart.setText("识别中"); Log.i("led","MusicActivity 识别中"); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: mBtnStart.setText("开始"); mTextView.setText("已取消"); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: mTextView.setText("错误代码:"+msg.arg1); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME: mTextViewVolume.setText("音量: "+msg.arg1); break; caseMessageConst.SERVER_ACTION_RETURN_RESULT: //mTextView.setText(msg.obj.toString()); mBtnStart.setText("开始"); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: mBtnStart.setText("开始"); mBookUtil =BookUtil.getInstance(); mBookUtil.play(msg.arg1); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: mMusicView.setMusicName(msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: int current = msg.arg1; int duration = msg.arg2; mMusicView.setProgress(current*100/duration); float time =duration/1000/60; mMusicView.setTotalTime("总时间:"+time); break; caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT: if(msg.obj != null) mInputTextView.setText("文字:"+msg.obj.toString()); break; caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE: if(msg.obj != null) mTextView.setText("服务器返回sentence:"+msg.obj.toString()); break; default: break; } } }; } private void initCommunicationAssist() {//向Application注册VoiceSdkService到MusicActivity的回调 mActivityComAssist = new ActivityComAssist(); OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist); } private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj) {//向VoiceSdkService发送消息 if(OlamiApplication.getInstance().getActivityToServiceListener() !=null) OlamiApplication.getInstance().getActivityToServiceListener().callBack (what, arg1, arg2, data, obj); } private class ActivityComAssist implements CommunicationAssist{ //实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息 @Override public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) { Message msg = Message.obtain(null, what); msg.arg1 = arg1; msg.arg2 = arg2; if (data != null) msg.setData(data); if (obj != null) msg.obj = obj; mInComingHandler.sendMessage(msg); } } @Override public void onDestroy() { //退出应用,停止VoiceSdkService,会进行资源的释放 super.onDestroy(); Intent intent = new Intent(); intent.setClass(MusicActivity.this, VoiceSdkService.class); stopService(intent); }}3.3VoiceSdkService@Overridepublic void onCreate() { initHandler();//用于内部消息处理 initInComingHandler();//用于处理来自MusicActivity的消息 initCommunicationAssist();//向application注册消息回调,这样MusicActivity可 //以通过getActivityToServiceListener()回调向VoiceSdkService发送消息 initViaVoiceRecognizerListener();//初始化录音识别回调listener init();//olami录音识别引擎初始化 initXmly();//喜马拉雅初始化} public void init(){ initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( (this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体 mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); //注册Appkey,在olami官网注册应用后生成的appkey //注册api,请直接填写“asr”,标识语音识别类型 //注册secret,在olami官网注册应用后生成的secret //注册seq ,请填写“nli” mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms //设置经纬度信息,不愿上传位置信息,可以填0 mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);} 定义OlamiVoiceRecognizerListeneronError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量下面是VoiceSdkService完整代码: public class VoiceSdkService extends Service{ private Handler mHandler; private Handler mInComingHandler; private VoiceSdkComAssist mVoiceSdkComAssist; private OlamiVoiceRecognizer mOlamiVoiceRecognizer; private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener; privateBookUtil mBookUtil = null; private boolean mIsRecordPause = false; @Override public void onCreate() { initHandler(); initInComingHandler(); initCommunicationAssist(); initViaVoiceRecognizerListener(); init(); initXmly(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void init() { initHandler(); mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this); TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService( this.getBaseContext().TELEPHONY_SERVICE); String imei=telephonyManager.getDeviceId(); mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server. mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener); mOlamiVoiceRecognizer.setLocalization( OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE); mOlamiVoiceRecognizer.setAuthorization( "51a4bb56ba954655a4fc834bfdc46af1", "asr","68bff251789b426896e70e888f919a6d","nli"); mOlamiVoiceRecognizer.setVADTailTimeout(2000); mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009); } private void initHandler() { mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null); break; case MessageConst.CLIENT_ACTION_STOP_RECORED: sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null); break; caseMessageConst.CLIENT_ACTION_ON_ERROR: sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null); break; caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH: sendMessageToActivity(MessageConst. CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj); break; caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS: sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: sendMessageToActivity(MessageConst. CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null); break; default: break; } } }; } private void initInComingHandler() { mInComingHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ caseMessageConst.CLIENT_ACTION_START_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.start(); break; caseMessageConst.CLIENT_ACTION_STOP_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.stop(); break; caseMessageConst.CLIENT_ACTION_CANCEL_RECORED: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.cancel(); break; case MessageConst.CLIENT_ACTION_SENT_TEXT: if(mOlamiVoiceRecognizer !=null) mOlamiVoiceRecognizer.sendText(msg.obj.toString()); break; } } }; } private void initViaVoiceRecognizerListener() { mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener(); } private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{ @Override public void onError(int errCode) { mHandler.sendMessage(mHandler.obtainMessage( MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0)); } @Override public void onEndOfSpeech() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED); if(mIsRecordPause) { mIsRecordPause = false; mBookUtil.resumePlay(); } } @Override public void onBeginningOfSpeech() { if(mBookUtil.isPlaying()) { mBookUtil.pause(); mIsRecordPause = true; } mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED); } @Override public void onResult(String result, int type) { sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result); processServiceMessage(result); } @Override public void onCancel() { mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED); } @Override public void onUpdateVolume(int volume) { sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null); } } private void initCommunicationAssist() { mVoiceSdkComAssist = new VoiceSdkComAssist(); OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist); } private void initXmly() { if(mBookUtil == null) { mBookUtil = BookUtil.getInstance(); mBookUtil.init(VoiceSdkService.this); mBookUtil.setHandler(mHandler); } } private void processServiceMessage(String message) { String input = null; String serverMessage = null; try{ JSONObject jsonObject = new JSONObject(message); JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli"); JSONObject jObj = jArrayNli.optJSONObject(0); JSONArray jArraySemantic = null; if(message.contains("semantic")) jArraySemantic =jObj.getJSONArray("semantic"); else{ input =jsonObject.optJSONObject("data").optJSONObject("asr"). optString("result"); sendMessageToActivity(MessageConst. CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input); 本文题目:语音识别,语义理解一站式解决(android平台&olamisdk) 转载来于:http://dzwzjz.com/article/pgocsj.html
android:id="@+id/btn_cancel"
android:layout_toRightOf="@+id/et_content"
android:text="取消" />
自定义MusicView比较简单,代码如下:
public class MusicView extendsRelativeLayout{
private Context mContext;
private Handler mHandler;
private TextView mTextViewName;
private TextView mTextViewTotalTime;
private ProgressBar mProgressBar;
public MusicView(Context context,AttributeSet attrs) {
super(context,attrs);
LayoutInflater inflater =(LayoutInflater) context.getSystemService(
context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) inflater.inflate
(R.layout.layout_musicview,this,true);
mTextViewName = (TextView) view.findViewById(R.id.tv_name);
mTextViewTotalTime = (TextView) view.findViewById(R.id.tv_total_time);
mProgressBar = (ProgressBar)view.findViewById(R.id.progressbar_music);
}
public void initMusicView(Context context,Handler handler)
{
mContext = context;
mHandler = handler;
public void setMusicName(String name)
{//设置播放名称
mTextViewName.setText(name);
public void setProgress(int progress)
{//设置播放进度
mProgressBar.setProgress(progress);
public void setTotalTime(String time)
{//设置播放总时间
mTextViewTotalTime.setText(time);
布局效果图如下:
本文没有用bindservice的方式实现activity和service的消息通信。 MusicAcitity和 VoiceSdkService中分别实现了一个CommunicationAssist的接口
public interface CommunicationAssist {
public void callBack(int what, int arg1, int arg2,Bundle data, Objectobj);
然后把他们分别实现CommunicationAssist接口的变量注册到OlamiApplication,这样通过OlamiApplication实现了MusicAcitity和VoiceSdkService桥接。
1)注册MusicActivity到VoiceSdkService的回调
public voidsetActivityToServiceListener(CommunicationAssist listener)
ActivityToServiceListener = listener;
这个是在VoiceSdkService中调用setActivityToServiceListener(),把VoiceSdkService中的VoiceSdkComAssist注册到application中,MusicActivity中可以通过getActivityToServiceListener 这个函数回调向VoiceSdkService发送消息。
2)注册 VoiceSdkService到MusicActivity回调
public voidsetServiceToActivityListener(CommunicationAssist listener)
mServiceToActivityListener = listener;
这个是在MusicAcitivity中调用setServiceToActivityListener(),这样在VoiceSdkService中就可以通过getServiceToActivityListener()获得回调向MusciActivity发送消息。
public class MusicActivity extends Activity{
private Handler mInComingHandler;
private ActivityComAssist mActivityComAssist;
private Button mBtnStart;
private Button mBtnStop;
private Button mBtnCancel;
private Button mBtnSend;
private EditText mEditText;
private TextView mTextView;
private TextView mInputTextView;
private TextView mTextViewVolume;
private BookUtil mBookUtil = null;
private MusicView mMusicView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
initHandler();//初始化handler用于内部消息处理
initInComingHandler();//用于处理来自VoiceSdkService的消息
initCommunicationAssist();//向application注册消息回调,VoiceSdkSerive可以
//通过getServiceToActivityListener()获得回调向MusicActivity发送消息
initView();//初始化view控件
Intent intent = new Intent();
intent.setClass(MusicActivity.this, VoiceSdkService.class);
startService(intent);//启动后台服务
private void initView()
mBtnStart = (Button) findViewById(R.id.btn_start);
mBtnStop = (Button) findViewById(R.id.btn_stop);
mBtnCancel = (Button) findViewById(R.id.btn_cancel);
mBtnSend = (Button) findViewById(R.id.btn_send);
mInputTextView = (TextView) findViewById(R.id.tv_inputText);
mEditText = (EditText) findViewById(R.id.et_content);
mTextView = (TextView) findViewById(R.id.tv_result);
mTextViewVolume = (TextView) findViewById(R.id.tv_volume);
mBtnStart.setOnClickListener(newOnClickListener(){
public void onClick(View v) {
sendMessageToService(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null);
});
mBtnStop.setOnClickListener(new OnClickListener(){
sendMessageToService(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null);
mBtnStart.setText("开始");
Log.i("led","MusicActivity mBtnStop onclick 开始");
mBtnCancel.setOnClickListener(new OnClickListener(){
sendMessageToService(MessageConst.CLIENT_ACTION_CANCEL_RECORED,0,0,null,null);
mBtnSend.setOnClickListener(new OnClickListener(){
sendMessageToService(
MessageConst.CLIENT_ACTION_SENT_TEXT,0,0,null,mEditText.getText());
mInputTextView.setText("文字:"+mEditText.getText());
mMusicView = (MusicView) findViewById(R.id.music_view);
//if(mMusicView != null)
//mMusicView.initMusicView(MusicActivity.this,mHandler);
private void initHandler()
mHandler = new Handler(){
public voidhandleMessage(Message msg)
switch (msg.what){
caseMessageConst.CLIENT_ACTION_START_RECORED:
break;
default:
};
//InComingHandler 收到来自VoiceSdkService的消息用于更新界面,
//包括开始录音,结束录音,播放的书的名称和进度,音量等信息。
private void initInComingHandler()
mInComingHandler = new Handler(){
public void handleMessage(Message msg)
mBtnStart.setText("录音中");
Log.i("led","MusicActivity 录音中");
caseMessageConst.CLIENT_ACTION_STOP_RECORED:
mBtnStart.setText("识别中");
Log.i("led","MusicActivity 识别中");
caseMessageConst.CLIENT_ACTION_CANCEL_RECORED:
mTextView.setText("已取消");
caseMessageConst.CLIENT_ACTION_ON_ERROR:
mTextView.setText("错误代码:"+msg.arg1);
caseMessageConst.CLIENT_ACTION_UPDATA_VOLUME:
mTextViewVolume.setText("音量: "+msg.arg1);
caseMessageConst.SERVER_ACTION_RETURN_RESULT:
//mTextView.setText(msg.obj.toString());
caseMessageConst.CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH:
mBookUtil =BookUtil.getInstance();
mBookUtil.play(msg.arg1);
caseMessageConst.CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME:
mMusicView.setMusicName(msg.obj.toString());
caseMessageConst.CLIENT_ACTION_UPDATE_BOOK_PROGRESS:
int current = msg.arg1;
int duration = msg.arg2;
mMusicView.setProgress(current*100/duration);
float time =duration/1000/60;
mMusicView.setTotalTime("总时间:"+time);
caseMessageConst.CLIENT_ACTION_UPDATA_INPUT_TEXT:
if(msg.obj != null)
mInputTextView.setText("文字:"+msg.obj.toString());
caseMessageConst.CLIENT_ACTION_UPDATA_SERVER_MESSAGE:
mTextView.setText("服务器返回sentence:"+msg.obj.toString());
private void initCommunicationAssist()
{//向Application注册VoiceSdkService到MusicActivity的回调
mActivityComAssist = new ActivityComAssist();
OlamiApplication.getInstance().setServiceToActivityListener(mActivityComAssist);
private void sendMessageToService(int what, int arg1, int arg2, Bundledata, Object obj)
{//向VoiceSdkService发送消息
if(OlamiApplication.getInstance().getActivityToServiceListener() !=null)
OlamiApplication.getInstance().getActivityToServiceListener().callBack
(what, arg1, arg2, data, obj);
private class ActivityComAssist implements CommunicationAssist{
//实现CommunicationAssist借口,用于回调VoiceSdkService发送过来的消息
public void callBack(int what, int arg1, int arg2, Bundle data,Objectobj) {
Message msg = Message.obtain(null, what);
msg.arg1 = arg1;
msg.arg2 = arg2;
if (data != null)
msg.setData(data);
if (obj != null)
msg.obj = obj;
mInComingHandler.sendMessage(msg);
public void onDestroy() {
//退出应用,停止VoiceSdkService,会进行资源的释放
super.onDestroy();
stopService(intent);
public void onCreate() {
initHandler();//用于内部消息处理
initInComingHandler();//用于处理来自MusicActivity的消息
initCommunicationAssist();//向application注册消息回调,这样MusicActivity可
//以通过getActivityToServiceListener()回调向VoiceSdkService发送消息
initViaVoiceRecognizerListener();//初始化录音识别回调listener
init();//olami录音识别引擎初始化
initXmly();//喜马拉雅初始化
public void init()
initHandler();
mOlamiVoiceRecognizer = new OlamiVoiceRecognizer(VoiceSdkService.this);
TelephonyManager telephonyManager=(TelephonyManager)this.getSystemService(
(this.getBaseContext().TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
mOlamiVoiceRecognizer.init(imei);//设置身份标识,可以填null
mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);//设置识别结果回调listener
mOlamiVoiceRecognizer.setLocalization(
OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);//设置支持的语音类型,优先选择中文简体
mOlamiVoiceRecognizer.setAuthorization("51a4bb56ba954655a4fc834bfdc46af1",
"asr","68bff251789b426896e70e888f919a6d","nli");
//注册Appkey,在olami官网注册应用后生成的appkey
//注册api,请直接填写“asr”,标识语音识别类型
//注册secret,在olami官网注册应用后生成的secret
//注册seq ,请填写“nli”
mOlamiVoiceRecognizer.setVADTailTimeout(2000);//录音时尾音结束时间,建议填//2000ms
//设置经纬度信息,不愿上传位置信息,可以填0
mOlamiVoiceRecognizer.setLatitudeAndLongitude(31.155364678184498,121.34882432933009);
定义OlamiVoiceRecognizerListener
onError(int errCode)//出错回调,可以对比官方文档错误码看是什么错误 onEndOfSpeech()//录音结束 onBeginningOfSpeech()//录音开始 onResult(String result, int type)//result是识别结果JSON字符串 onCancel()//取消识别,不会再返回识别结果 onUpdateVolume(int volume)//录音时的音量,1-12个级别大小音量
下面是VoiceSdkService完整代码:
public class VoiceSdkService extends Service{
private VoiceSdkComAssist mVoiceSdkComAssist;
private OlamiVoiceRecognizer mOlamiVoiceRecognizer;
private OlamiVoiceRecognizerListener mOlamiVoiceRecognizerListener;
privateBookUtil mBookUtil = null;
private boolean mIsRecordPause = false;
initInComingHandler();
initCommunicationAssist();
initViaVoiceRecognizerListener();
init();
initXmly();
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
this.getBaseContext().TELEPHONY_SERVICE);
mOlamiVoiceRecognizer.init(imei);//set null if you do not want to notifyolami server.
mOlamiVoiceRecognizer.setListener(mOlamiVoiceRecognizerListener);
OlamiVoiceRecognizer.LANGUAGE_SIMPLIFIED_CHINESE);
mOlamiVoiceRecognizer.setAuthorization(
"51a4bb56ba954655a4fc834bfdc46af1",
mOlamiVoiceRecognizer.setVADTailTimeout(2000);
sendMessageToActivity(MessageConst.CLIENT_ACTION_START_RECORED,0,0,null,null);
case MessageConst.CLIENT_ACTION_STOP_RECORED:
sendMessageToActivity(MessageConst.CLIENT_ACTION_STOP_RECORED,0,0,null,null);
sendMessageToActivity(MessageConst.CLIENT_ACTION_ON_ERROR,msg.arg1,0,null,null);
sendMessageToActivity(MessageConst.
CLIENT_ACTION_PLAY_BOOK_AFTER_SEARCH,msg.arg1, 0, null, msg.obj);
CLIENT_ACTION_UPDATA_PLAYING_BOOK_NAME, msg.arg1, 0, null, msg.obj);
CLIENT_ACTION_UPDATE_BOOK_PROGRESS,msg.arg1, msg.arg2, null, null);
CLIENT_ACTION_CANCEL_RECORED,msg.arg1, msg.arg2, null, null);
if(mOlamiVoiceRecognizer !=null)
mOlamiVoiceRecognizer.start();
mOlamiVoiceRecognizer.stop();
mOlamiVoiceRecognizer.cancel();
case MessageConst.CLIENT_ACTION_SENT_TEXT:
mOlamiVoiceRecognizer.sendText(msg.obj.toString());
private void initViaVoiceRecognizerListener()
mOlamiVoiceRecognizerListener = new OlamiVoiceRecognizerListener();
private class OlamiVoiceRecognizerListener implementsIOlamiVoiceRecognizerListener{
public void onError(int errCode) {
mHandler.sendMessage(mHandler.obtainMessage(
MessageConst.CLIENT_ACTION_ON_ERROR,errCode,0));
public void onEndOfSpeech() {
mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_STOP_RECORED);
if(mIsRecordPause)
mIsRecordPause = false;
mBookUtil.resumePlay();
public void onBeginningOfSpeech() {
if(mBookUtil.isPlaying())
mBookUtil.pause();
mIsRecordPause = true;
mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_START_RECORED);
public void onResult(String result, int type) {
sendMessageToActivity(MessageConst.SERVER_ACTION_RETURN_RESULT,type,0,null,result);
processServiceMessage(result);
public void onCancel() {
mHandler.sendEmptyMessage(MessageConst.CLIENT_ACTION_CANCEL_RECORED);
public void onUpdateVolume(int volume) {
sendMessageToActivity(MessageConst.CLIENT_ACTION_UPDATA_VOLUME,volume,0,null,null);
mVoiceSdkComAssist = new VoiceSdkComAssist();
OlamiApplication.getInstance().setActivityToServiceListener(mVoiceSdkComAssist);
private void initXmly()
if(mBookUtil == null)
mBookUtil = BookUtil.getInstance();
mBookUtil.init(VoiceSdkService.this);
mBookUtil.setHandler(mHandler);
private void processServiceMessage(String message)
String input = null;
String serverMessage = null;
try{
JSONObject jsonObject = new JSONObject(message);
JSONArray jArrayNli = jsonObject.optJSONObject("data").optJSONArray("nli");
JSONObject jObj = jArrayNli.optJSONObject(0);
JSONArray jArraySemantic = null;
if(message.contains("semantic"))
jArraySemantic =jObj.getJSONArray("semantic");
else{
input =jsonObject.optJSONObject("data").optJSONObject("asr").
optString("result");
CLIENT_ACTION_UPDATA_INPUT_TEXT, 0, 0, null, input);