安卓Android科大訊飛語音識別代碼使用詳解


科大訊飛的語音識別功能用在安卓代碼中,我把語音識別寫成了Service,然后在Fragment直接調用service服務。科大訊飛語音識別用的是帶對話框的那個,直接調用科大訊飛的語音接口,代碼采用鏈表結果集的方式獲取數據。 
這個語音識別需要在官網申請APPID

本博來自:http://blog.csdn.net/zhaocundang 小波LinuxQQ463431476

測試:

這里寫圖片描述

這里寫圖片描述

自己項目采用了科大訊飛語音識別服務,報告中是這樣解釋的:

語音Service服務代碼設計

(1)要想寫好Service代碼,必須了解Service的生命周期.

(2)首先啟動Service服務的方法是: 
getActivity().startService(new Intent(getActivity(),VoiceService.class)); 
停止Service服務: 
getActivity().stopService(new Intent(getActivity(),VoiceService.class)); 
(3)將類繼承與Service: 
public class VoiceService extends Service{ 
} 
自動重載OnBind()函數,通過OnBind()的返回值,將Service的實例返回調用者。 
(3) 調用科大訊飛語音API接口代碼 
先調用手機麥克風錄音: 
rd.setSampleRate(RATE.rate16k); 
調用語音API包中的語音識別對話框,將錄音發送到服務器,並接受服務器返回的結果,將數據以鏈表數據結構的形式傳過來,獲取結果: 
final StringBuilder sb = new StringBuilder(); 
rd.setListener(new RecognizerDialogListener() { 
public void onResults(ArrayList result, boolean isLast) { 
for (RecognizerResult recognizerResult : result) { 
sb.append(recognizerResult.text); 
} 
} 
public void onEnd(SpeechError error) { 
} 
}); 
(4)文本語音朗讀的調用: 
先是聲明播放對象: 
private static SynthesizerPlayer player ; 
這里我直接封裝一個朗讀函數,appid是申請的應用授權id,代碼如下: 
public void speak(String words){ 
player = SynthesizerPlayer.createSynthesizerPlayer(getActivity(),”appid=57527406”); 
player.playText(words, null,null); //播放文本 
}

主要的代碼:

開啟和關閉服務:


public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.button1: getActivity().startService(new Intent(getActivity(),VoiceService.class)); break; case R.id.button2: getActivity().stopService(new Intent(getActivity(),VoiceService.class)); break; } } 

服務中:

package zcd.voice; import java.util.ArrayList; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.view.WindowManager; import android.widget.Toast; import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechConfig.RATE; import com.iflytek.speech.SpeechError; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; public class VoiceService extends Service{ private RecognizerDialog rd; private String text; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // Toast.makeText(this, "Service onCreated", Toast.LENGTH_LONG).show(); rd = new RecognizerDialog(this ,"appid=57627d9c"); } public void onStart(Intent intent, int startId) { // Toast.makeText(this, " Service onStart", Toast.LENGTH_LONG).show(); showReconigizerDialog(); } private void showReconigizerDialog() { //sms 簡單語音識別文本 rd.setEngine("sms", null, null); //設置麥克風采樣頻率 rd.setSampleRate(RATE.rate16k); final StringBuilder re = new StringBuilder(); //設置識別后的回調結果 rd.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> result, boolean isLast) { for (RecognizerResult recognizerResult : result) { re.append(recognizerResult.text); } } @Override public void onEnd(SpeechError error) { //識別完成 //R.id.txt_result.setText(sb.toString()); text = re.toString(); Toast.makeText(VoiceService.this,re.toString(), Toast.LENGTH_LONG).show(); sendmsg(); } }); //txt_result.setText(""); //先設置為空,等識別完成后設置內容 rd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // service 中getwindowmanager 設置優先級顯示對話框 rd.show(); } public void sendmsg() { //broadcast // service 通過廣播來發送識別結果到Voice Fragment Intent intent=new Intent(); intent.putExtra("message",text); intent.setAction("zcd.voice"); sendBroadcast(intent); } } 

Service中是無法顯示對話框的,顯示對話框的方式就是使用getwindow的方法,設置窗口最高優先級即可了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM