官方文檔:https://doc.xfyun.cn/msc_android/index.htmlhtml
Demo:java
1.建立應用,獲取AppIdandroid
2.下載SDK,集成json
3.
數組
4.代碼實現session
-
import android.app.Activity ;
-
import android.os.Bundle ;
-
import android.util.Log ;
-
import android.view.View ;
-
import android.widget.Button ;
-
import android.widget.EditText ;
-
import android.widget.Toast ;
-
-
import com.iflytek.cloud.ErrorCode ;
-
import com.iflytek.cloud.InitListener ;
-
import com.iflytek.cloud.RecognizerListener ;
-
import com.iflytek.cloud.RecognizerResult ;
-
import com.iflytek.cloud.SpeechConstant ;
-
import com.iflytek.cloud.SpeechError ;
-
import com.iflytek.cloud.SpeechRecognizer ;
-
import com.iflytek.cloud.SpeechSynthesizer ;
-
import com.iflytek.cloud.SpeechUtility ;
-
import com.iflytek.cloud.SynthesizerListener ;
-
import com.iflytek.cloud.ui.RecognizerDialog ;
-
import com.iflytek.cloud.ui.RecognizerDialogListener ;
-
-
import org.json.JSONException ;
-
import org.json.JSONObject ;
-
-
import java.util.HashMap ;
-
import java.util.LinkedHashMap ;
-
-
public class MainActivity extends Activity implements View.OnClickListener {
-
-
private static final String TAG = MainActivity.class .getSimpleName();
-
private EditText et_input;
-
private Button btn_startspeech, btn_startspeektext ;
-
-
-
private HashMap<String, String> mIatResults = new LinkedHashMap<String , String>();
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super .onCreate(savedInstanceState) ;
-
-
-
-
-
private void initView() {
-
setContentView(R.layout.activity_main) ;
-
et_input = (EditText) findViewById(R.id.et_input );
-
btn_startspeech = (Button) findViewById(R.id.btn_startspeech );
-
btn_startspeektext = (Button) findViewById(R.id.btn_startspeektext );
-
btn_startspeech .setOnClickListener(this) ;
-
btn_startspeektext .setOnClickListener(this) ;
-
-
-
private void initSpeech() {
-
-
-
SpeechUtility. createUtility( this, SpeechConstant. APPID + "=56ffe0ae" );
-
-
-
-
public void onClick(View v) {
-
-
case R.id.btn_startspeech:
-
-
-
case R.id. btn_startspeektext:
-
-
-
-
-
-
-
private void speekText() {
-
-
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer( this, null);
-
-
-
mTts.setParameter(SpeechConstant. VOICE_NAME, "vixyun" );
-
mTts.setParameter(SpeechConstant. SPEED, "50" );
-
mTts.setParameter(SpeechConstant. VOLUME, "80" );
-
mTts.setParameter(SpeechConstant. ENGINE_TYPE, SpeechConstant. TYPE_CLOUD);
-
-
-
-
mTts.setParameter(SpeechConstant. TTS_AUDIO_PATH, "./sdcard/iflytek.pcm" );
-
-
mTts.startSpeaking( et_input.getText().toString(), new MySynthesizerListener()) ;
-
-
-
-
class MySynthesizerListener implements SynthesizerListener {
-
-
-
public void onSpeakBegin() {
-
-
-
-
-
public void onSpeakPaused() {
-
-
-
-
-
public void onSpeakResumed() {
-
-
-
-
-
public void onBufferProgress(int percent, int beginPos, int endPos ,
-
-
-
-
-
-
public void onSpeakProgress(int percent, int beginPos, int endPos) {
-
-
-
-
-
public void onCompleted(SpeechError error) {
-
-
-
} else if (error != null ) {
-
showTip(error.getPlainDescription( true));
-
-
-
-
-
public void onEvent(int eventType, int arg1 , int arg2, Bundle obj) {
-
-
-
-
-
-
-
-
-
-
private void startSpeechDialog() {
-
-
RecognizerDialog mDialog = new RecognizerDialog(this, new MyInitListener()) ;
-
-
mDialog.setParameter(SpeechConstant. LANGUAGE, "zh_cn" );
-
mDialog.setParameter(SpeechConstant. ACCENT, "mandarin" );
-
-
-
-
-
-
mDialog.setListener( new MyRecognizerDialogListener()) ;
-
-
-
-
-
class MyRecognizerDialogListener implements RecognizerDialogListener {
-
-
-
-
-
-
-
public void onResult(RecognizerResult results, boolean isLast) {
-
String result = results.getResultString();
-
-
System. out.println(" 沒有解析的 :" + result);
-
-
String text = JsonParser.parseIatResult(result) ;
-
System. out.println(" 解析後的 :" + text);
-
-
-
-
-
JSONObject resultJson = new JSONObject(results.getResultString()) ;
-
sn = resultJson.optString("sn" );
-
} catch (JSONException e) {
-
-
-
-
mIatResults .put(sn, text) ;
-
-
StringBuffer resultBuffer = new StringBuffer();
-
for (String key : mIatResults.keySet()) {
-
resultBuffer.append(mIatResults .get(key));
-
-
-
et_input.setText(resultBuffer.toString());
-
et_input .setSelection(et_input.length()) ;
-
-
-
-
public void onError(SpeechError speechError) {
-
-
-
-
-
class MyInitListener implements InitListener {
-
-
-
public void onInit(int code) {
-
if (code != ErrorCode.SUCCESS) {
-
-
-
-
-
-
-
-
-
-
private void startSpeech() {
-
-
SpeechRecognizer mIat = SpeechRecognizer.createRecognizer( this, null);
-
-
mIat.setParameter(SpeechConstant. DOMAIN, "iat" );
-
mIat.setParameter(SpeechConstant. LANGUAGE, "zh_cn" );
-
mIat.setParameter(SpeechConstant. ACCENT, "mandarin" );
-
-
mIat.startListening( mRecoListener);
-
-
-
-
-
private RecognizerListener mRecoListener = new RecognizerListener() {
-
-
-
-
-
public void onResult(RecognizerResult results, boolean isLast) {
-
Log.e (TAG, results.getResultString());
-
System.out.println(results.getResultString()) ;
-
showTip(results.getResultString()) ;
-
-
-
-
public void onError(SpeechError error) {
-
showTip(error.getPlainDescription(true)) ;
-
-
Log. e(TAG, "error.getPlainDescription(true)==" + error.getPlainDescription(true ));
-
-
-
-
public void onBeginOfSpeech() {
-
-
-
-
-
public void onVolumeChanged(int volume, byte[] data) {
-
-
-
-
-
public void onEndOfSpeech() {
-
-
-
-
-
public void onEvent(int eventType, int arg1 , int arg2, Bundle obj) {
-
-
-
-
private void showTip (String data) {
-
Toast.makeText( this, data, Toast.LENGTH_SHORT).show() ;
-
}
7.Json實體類
-
import org.json.JSONArray
-
import org.json.JSONObject
-
import org.json.JSONTokener
-
-
-
-
-
public class JsonParser {
-
-
public static String parseIatResult(String json) {
-
StringBuffer ret = new StringBuffer()
-
-
JSONTokener tokener = new JSONTokener(json)
-
JSONObject joResult = new JSONObject(tokener)
-
-
JSONArray words = joResult.getJSONArray("ws" )
-
-
-
JSONArray items = words.getJSONObject(i).getJSONArray("cw" )
-
JSONObject obj = items.getJSONObject(0 )
-
ret.append(obj.getString("w" ))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static String parseGrammarResult(String json) {
-
StringBuffer ret = new StringBuffer()
-
-
JSONTokener tokener = new JSONTokener(json)
-
JSONObject joResult = new JSONObject(tokener)
-
-
JSONArray words = joResult.getJSONArray("ws" )
-
-
JSONArray items = words.getJSONObject(i).getJSONArray("cw" )
-
-
-
JSONObject obj = items.getJSONObject(j)
-
if (obj.getString("w").contains( "nomatch"))
-
-
-
-
-
ret.append( "【結果】" + obj.getString("w" ))
-
ret.append("【置信度】 " + obj.getInt("sc" ))
-
-
-
-
-
-
-
-
-
-
-
public static String parseLocalGrammarResult(String json) {
-
StringBuffer ret = new StringBuffer()
-
-
JSONTokener tokener = new JSONTokener(json)
-
JSONObject joResult = new JSONObject(tokener)
-
-
JSONArray words = joResult.getJSONArray("ws" )
-
-
JSONArray items = words.getJSONObject(i).getJSONArray("cw" )
-
-
-
JSONObject obj = items.getJSONObject(j)
-
if (obj.getString("w").contains( "nomatch"))
-
-
-
-
-
ret.append( "【結果】" + obj.getString("w" ))
-
-
-
-
ret.append("【置信度】 " + joResult.optInt("sc" ))
-
-
-
-
-
-
-