content: package com.handsome.didi.Utils;
  
  import android.content.Context;
  import android.content.Intent;
  
  import com.google.gson.Gson;
  import com.handsome.didi.Activity.Home.SearchActivity;
  import com.handsome.didi.Bean.Voice;
  import com.handsome.didi.Controller.ActivityController;
  import com.iflytek.cloud.RecognizerResult;
  import com.iflytek.cloud.SpeechConstant;
  import com.iflytek.cloud.SpeechError;
  import com.iflytek.cloud.ui.RecognizerDialog;
  import com.iflytek.cloud.ui.RecognizerDialogListener;
  
  import java.util.ArrayList;
  
  /**
   * Created by handsome on 2016/4/17.
   */
  public class SpeechUtils {
  
      /**
       * 初始化语音识别
       */
      public static void initSpeech(final Context context) {
          //1.创建RecognizerDialog对象
          RecognizerDialog mDialog = new RecognizerDialog(context, null);
          //2.设置accent、language等参数
          mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
          mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
          //3.设置回调接口
          mDialog.setListener(new RecognizerDialogListener() {
              @Override
              public void onResult(RecognizerResult recognizerResult, boolean isLast) {
                  if (!isLast) {
                      //解析语音
                      String result = parseVoice(recognizerResult.getResultString());
                      //开启搜索页面
                      Intent intent = new Intent(context, SearchActivity.class);
                      intent.putExtra("content", result);
                      context.startActivity(intent);
                  }
              }
  
              @Override
              public void onError(SpeechError speechError) {
  
              }
          });
          //4.显示dialog，接收语音输入
          mDialog.show();
      }
  
      /**
       * 解析语音json
       *
       * @param resultString
       * @return
       */
      public static String parseVoice(String resultString) {
          Gson gson = new Gson();
          Voice voiceBean = gson.fromJson(resultString, Voice.class);
  
          StringBuffer sb = new StringBuffer();
          ArrayList<Voice.WSBean> ws = voiceBean.ws;
          for (Voice.WSBean wsBean : ws) {
              String word = wsBean.cw.get(0).w;
              sb.append(word);
          }
          return sb.toString();
      }
  }
  