소스 검색

语音识别版本1.0,有闪退,后面考虑是否需要

FANGLH 9 년 전
부모
커밋
b625ddad88

+ 1 - 0
WeiChat/build.gradle

@@ -132,4 +132,5 @@ dependencies {
     compile files('libs/lite-orm-1.7.0.jar')
     //    compile fileTree(include: ['*.jar'], dir: 'libs')
     testCompile 'junit:junit:4.12'
+    compile files('libs/Msc.jar')
 }

BIN
WeiChat/libs/Msc.jar


+ 1 - 0
WeiChat/src/main/AndroidManifest.xml

@@ -826,6 +826,7 @@
         </receiver>
 
         <activity android:name=".ui.erp.activity.oa.ErpActivity"></activity>
+        <activity android:name=".ui.me.SpeechrecognitionActivity"></activity>
     </application>
 
 </manifest>

+ 1 - 1
WeiChat/src/main/java/com/xzjmyk/pm/activity/MyApplication.java

@@ -298,7 +298,7 @@ public class MyApplication extends Application {
         ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(mNormalImageOptions)
                 // .denyCacheImageMultipleSizesInMemory()
                 .discCache(new TotalSizeLimitedDiscCache(new File(mPicturesDir), 50 * 1024 * 1024))
-                // 最多缓存50M的图片
+                        // 最多缓存50M的图片
                 .discCacheFileNameGenerator(new Md5FileNameGenerator()).memoryCache(memoryCache).tasksProcessingOrder(QueueProcessingType.LIFO)
                 .threadPriority(Thread.NORM_PRIORITY - 2)
                 .threadPoolSize(4)

+ 103 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/audio/voicerecognition/JsonParser.java

@@ -0,0 +1,103 @@
+package com.xzjmyk.pm.activity.audio.voicerecognition;
+import android.text.TextUtils;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+/**
+ * Created by FANGlh on 2017/1/11.
+ * function: 对云端返回的Json结果进行解析
+ */
+public class JsonParser {
+
+	/**
+	 * 听写结果的Json格式解析
+	 * @param json
+	 * @return
+	 */
+	public static String parseIatResult(String json) {
+		if(TextUtils.isEmpty(json))
+			return "";
+
+		StringBuffer ret = new StringBuffer();
+		try {
+			JSONTokener tokener = new JSONTokener(json);
+			JSONObject joResult = new JSONObject(tokener);
+
+			JSONArray words = joResult.getJSONArray("ws");
+			for (int i = 0; i < words.length(); i++) {
+				// 听写结果词,默认使用第一个结果
+				JSONArray items = words.getJSONObject(i).getJSONArray("cw");
+				JSONObject obj = items.getJSONObject(0);
+				ret.append(obj.getString("w"));
+//				如果需要多候选结果,解析数组其他字段
+//				for(int j = 0; j < items.length(); j++)
+//				{
+//					JSONObject obj = items.getJSONObject(j);
+//					ret.append(obj.getString("w"));
+//				}
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return ret.toString();
+	}
+
+	/**
+	 * 识别结果的Json格式解析
+	 * @param json
+	 * @return
+	 */
+	public static String parseGrammarResult(String json) {
+		StringBuffer ret = new StringBuffer();
+		try {
+			JSONTokener tokener = new JSONTokener(json);
+			JSONObject joResult = new JSONObject(tokener);
+
+			JSONArray words = joResult.getJSONArray("ws");
+			for (int i = 0; i < words.length(); i++) {
+				JSONArray items = words.getJSONObject(i).getJSONArray("cw");
+				for(int j = 0; j < items.length(); j++)
+				{
+					JSONObject obj = items.getJSONObject(j);
+					if(obj.getString("w").contains("nomatch"))
+					{
+						ret.append("没有匹配结果.");
+						return ret.toString();
+					}
+					ret.append("【结果】" + obj.getString("w"));
+					ret.append("【置信度】" + obj.getInt("sc"));
+					ret.append("\n");
+				}
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			ret.append("没有匹配结果.");
+		}
+		return ret.toString();
+	}
+
+	/**
+	 * 语义结果的Json格式解析
+	 * @param json
+	 * @return
+	 */
+	public static String parseUnderstandResult(String json) {
+		StringBuffer ret = new StringBuffer();
+		try {
+			JSONTokener tokener = new JSONTokener(json);
+			JSONObject joResult = new JSONObject(tokener);
+
+			ret.append("【应答码】" + joResult.getString("rc") + "\n");
+			ret.append("【转写结果】" + joResult.getString("text") + "\n");
+			ret.append("【服务名称】" + joResult.getString("service") + "\n");
+			ret.append("【操作名称】" + joResult.getString("operation") + "\n");
+			ret.append("【完整结果】" + json);
+		} catch (Exception e) {
+			e.printStackTrace();
+			ret.append("没有匹配结果.");
+		}
+		return ret.toString();
+	}
+}

+ 45 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/audio/voicerecognition/MyRecognizerDialogLister.java

@@ -0,0 +1,45 @@
+package com.xzjmyk.pm.activity.audio.voicerecognition;
+
+import android.content.Context;
+import android.widget.Toast;
+
+import com.iflytek.cloud.speech.RecognizerResult;
+import com.iflytek.cloud.speech.SpeechError;
+import com.iflytek.cloud.ui.RecognizerDialogListener;
+
+/**
+ * Created by FANGlh on 2017/1/12.
+ * function:
+ */
+public class MyRecognizerDialogLister implements RecognizerDialogListener {
+    Context mContext;
+    public MyRecognizerDialogLister(Context mContext){
+        this.mContext  = mContext;
+    }
+    //自定义的结果回调函数,成功执行第一个方法,失败执行第二个方法
+    @Override
+    public void onResult(RecognizerResult results, boolean isLast) {
+        // TODO Auto-generated method stub
+        String text = JsonParser.parseIatResult(results.getResultString());
+        System.out.println(text);
+        Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
+    }
+    /**
+     * 识别回调错误.
+     */
+    @Override
+    public void onError(SpeechError error) {
+        // TODO Auto-generated method stub
+        int errorCoder = error.getErrorCode();
+        switch (errorCoder) {
+            case 10118:
+                System.out.println("user don't speak anything");
+                break;
+            case 10204:
+                System.out.println("can't connect to internet");
+                break;
+            default:
+                break;
+        }
+    }
+}

+ 156 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/audio/voicerecognition/VoiceToWord.java

@@ -0,0 +1,156 @@
+package com.xzjmyk.pm.activity.audio.voicerecognition;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.widget.Toast;
+
+import com.iflytek.cloud.speech.SpeechConstant;
+import com.iflytek.cloud.speech.SpeechError;
+import com.iflytek.cloud.speech.SpeechListener;
+import com.iflytek.cloud.speech.SpeechRecognizer;
+import com.iflytek.cloud.speech.SpeechUser;
+import com.iflytek.cloud.ui.RecognizerDialog;
+import com.iflytek.cloud.ui.RecognizerDialogListener;
+import com.xzjmyk.pm.activity.audio.voicerecognition.MyRecognizerDialogLister;
+
+public class VoiceToWord extends Activity{
+	private Context context;
+	private Toast mToast;
+	//识别窗口
+	private RecognizerDialog iatDialog;
+	//识别对象
+	private SpeechRecognizer iatRecognizer;
+	//缓存,保存当前的引擎参数到下一次启动应用程序使用.
+	private SharedPreferences mSharedPreferences;
+	private RecognizerDialogListener recognizerDialogListener = null;
+
+	public VoiceToWord(Context context,String APP_ID) {
+		// TODO Auto-generated constructor stub
+		//用户登录
+		this.context = context;
+		SpeechUser.getUser().login(context, null, null
+				, "appid=" + APP_ID, listener);
+		//初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
+		iatDialog =new RecognizerDialog(context);
+		mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
+		//初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
+		iatDialog =new RecognizerDialog(context);
+		//初始化缓存对象.
+		mSharedPreferences = context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
+	}
+
+	public VoiceToWord(Context context,String APP_ID,RecognizerDialogListener recognizerDialogListener)
+	{
+		this.context = context;
+		SpeechUser.getUser().login(context, null, null
+				, "appid=" + APP_ID, listener);
+		//初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
+		iatDialog =new RecognizerDialog(context);
+		mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
+		//初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
+		iatDialog =new RecognizerDialog(context);
+		//初始化缓存对象.
+		mSharedPreferences = context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
+		this.recognizerDialogListener = recognizerDialogListener;
+	}
+
+	public void GetWordFromVoice()
+	{
+		boolean isShowDialog = mSharedPreferences.getBoolean("iat_show",true);
+		if (isShowDialog) {
+			//显示语音听写Dialog.
+			showIatDialog();
+		} else {
+			if(null == iatRecognizer) {
+				iatRecognizer=SpeechRecognizer.createRecognizer(this);
+			}
+			if(iatRecognizer.isListening()) {
+				iatRecognizer.stopListening();
+//				((Button) findViewById(android.R.id.button1)).setEnabled(false);
+			} else {
+			}
+		}
+	}
+
+
+	private void showTip(String str)
+	{
+		if(!TextUtils.isEmpty(str))
+		{
+			mToast.setText(str);
+			mToast.show();
+		}
+	}
+	/**
+	 * 显示听写对话框.
+	 * @param
+	 */
+	public void showIatDialog()
+	{
+		if(null == iatDialog) {
+			//初始化听写Dialog
+			iatDialog =new RecognizerDialog(this);
+		}
+
+		//获取引擎参数
+		String engine = mSharedPreferences.getString(
+				"iat_engine",
+				"iat");
+
+		//清空Grammar_ID,防止识别后进行听写时Grammar_ID的干扰
+		iatDialog.setParameter(SpeechConstant.CLOUD_GRAMMAR, null);
+		//设置听写Dialog的引擎
+		iatDialog.setParameter(SpeechConstant.DOMAIN, engine);
+		//设置采样率参数,支持8K和16K 
+		String rate = mSharedPreferences.getString(
+				"sf",
+				"sf");
+		if(rate.equals("rate8k"))
+		{
+			iatDialog.setParameter(SpeechConstant.SAMPLE_RATE, "8000");
+		}
+		else
+		{
+			iatDialog.setParameter(SpeechConstant.SAMPLE_RATE, "16000");
+		}
+		if(recognizerDialogListener == null)
+		{
+			getRecognizerDialogListener();
+		}
+		//显示听写对话框
+		iatDialog.setListener(recognizerDialogListener);
+		iatDialog.show();
+	}
+	private void getRecognizerDialogListener()
+	{
+		/**
+		 * 识别回调监听器
+		 */
+		recognizerDialogListener=new MyRecognizerDialogLister(context);
+	}
+
+	/**
+	 * 用户登录回调监听器.
+	 */
+	private SpeechListener listener = new SpeechListener()
+	{
+
+		@Override
+		public void onData(byte[] arg0) {
+		}
+
+		@Override
+		public void onCompleted(SpeechError error) {
+			if(error != null) {
+				System.out.println("user login success");
+			}
+		}
+
+		@Override
+		public void onEvent(int arg0, Bundle arg1) {
+		}
+	};
+}

+ 7 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/me/SettingActivity.java

@@ -74,6 +74,8 @@ public class SettingActivity extends ActionBackActivity implements View.OnClickL
     private TextView version_value;
     @ViewInject(R.id.close_push_rl)
     private RelativeLayout close_push_rl;
+    @ViewInject(R.id.speech_recognition_rl)
+    private RelativeLayout speech_recognition_rl;
     private ProgressDialog dialog;
     private RelativeLayout share_rl;
     private String uustep_service_name = "com.xzjmyk.pm.basepedo.service.StepService";
@@ -173,6 +175,8 @@ public class SettingActivity extends ActionBackActivity implements View.OnClickL
                 }
             }
         });
+
+        speech_recognition_rl.setOnClickListener(this);
     }
 
     private boolean isServiceRunning(String servicename) { // 判断某个服务是否已经运行
@@ -219,6 +223,9 @@ public class SettingActivity extends ActionBackActivity implements View.OnClickL
     @Override
     public void onClick(View v) {
         switch (v.getId()) {
+            case R.id.speech_recognition_rl:
+                startActivity(new Intent(mContext,SpeechrecognitionActivity.class));
+                break;
             case  R.id.close_push_rl:
                 break;
             case R.id.clear_cache_rl:

+ 37 - 0
WeiChat/src/main/java/com/xzjmyk/pm/activity/ui/me/SpeechrecognitionActivity.java

@@ -0,0 +1,37 @@
+package com.xzjmyk.pm.activity.ui.me;
+
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+
+import com.xzjmyk.pm.activity.R;
+import com.xzjmyk.pm.activity.audio.voicerecognition.VoiceToWord;
+import com.xzjmyk.pm.activity.ui.base.BaseActivity;
+
+/**
+ * Created by FANGlh on 2017/1/11.
+ * function:
+ */
+public class SpeechrecognitionActivity extends BaseActivity {
+
+    private EditText identify_words_et;
+    private Button speak_start_bt;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.speech_recognition);
+
+        identify_words_et = (EditText) findViewById(R.id.sr_identify_words_et);
+        speak_start_bt = (Button) findViewById(R.id.sr_speak_start_bt);
+
+        speak_start_bt.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                VoiceToWord voice = new VoiceToWord(SpeechrecognitionActivity.this,"534e3fe2");
+                voice.GetWordFromVoice();
+            }
+        });
+    }
+}

+ 27 - 0
WeiChat/src/main/res/layout/activity_setting.xml

@@ -52,6 +52,33 @@
                     android:background="@drawable/oa_next"
                     android:contentDescription="@string/app_name" />
             </RelativeLayout>
+            <RelativeLayout
+                android:id="@+id/speech_recognition_rl"
+                style="@style/IMTbleLine"
+                android:background="@drawable/selector_me_menu_item_bg">
+
+                <TextView
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_centerVertical="true"
+                    android:gravity="center"
+                    android:text="语音识别"
+                    android:textColor="@color/black"
+                    android:textSize="14sp" />
+
+                <ImageView
+                    android:layout_width="@dimen/next_width"
+                    android:layout_height="@dimen/next_height"
+                    android:layout_alignParentRight="true"
+                    android:layout_centerVertical="true"
+
+                    android:background="@drawable/oa_next"
+                    android:contentDescription="@string/app_name" />
+            </RelativeLayout>
+            <View
+                android:layout_width="match_parent"
+                android:layout_height="@dimen/line"
+                android:background="@color/item_line" />
             <RelativeLayout
                 android:id="@+id/uu_step_rl"
                 style="@style/IMTbleLine1"

+ 22 - 0
WeiChat/src/main/res/layout/speech_recognition.xml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+    <EditText
+        android:id="@+id/sr_identify_words_et"
+        android:layout_width="fill_parent"
+        android:layout_height="300dp"
+        android:gravity="top"
+        android:inputType="textMultiLine" >
+
+        <requestFocus />
+    </EditText>
+
+    <Button
+        android:id="@+id/sr_speak_start_bt"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="点击开始说话"
+        android:layout_gravity="center"/>
+</LinearLayout>