TTS与google语言之别

简单的尝试有不一样的收获,是惊喜的,三星中文TTS与google语音输入的结合,可实现用户语音控制手机,可语音调用手机内各种APP,解放双手,并可与之聊天。。。

部分代码如下:

package com.mycompany.myapp15;

//TTS部分

 import java.util.Locale;

 import android.app.Activity;

import android.os.Bundle;

 import android.speech.tts.TextToSpeech;

 import android.text.Editable;

 import android.text.TextWatcher;

import android.view.View;

 import android.widget.Button;

 import android.widget.CheckBox;

 import android.widget.EditText;

import android.widget.Toast;

 //语音识别部分

import android.content.Intent;

import android.content.pm.PackageManager;

 import android.content.pm.ResolveInfo;

 import android.os.Bundle;

 import android.speech.RecognizerIntent;

 import android.view.View.OnClickListener;

 import android.widget.ArrayAdapter;

 import android.widget.ListView;

import java.util.ArrayList;

import java.util.List; import android.view.*;

 import android.app.*;

import android.net.*;

import android.text.*;

 import java.util.concurrent.*;

 import android.sax.*;

import android.test.*;

import android.content.res.*;

import android.provider.*;

 import java.io.*;

import android.os.*;

import android.content.*;

import java.util.*;

import android.database.*;

 import android.telephony.*;

public class MainActivity extends Activity {

//TTS

 private EditText mEditText = null;

private Button readButton = null; private Button saveButton = null; private CheckBox mCheckBox = null; private TextToSpeech mTextToSpeech = null; //speaking private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; private ListView mList; private ArrayList matches=null; private TextView sp_antext=null; public static final int PHOTOHRAPH = 1;// 拍照 public static final String IMAGE_UNSPECIFIED = "image/*"; //电话 public static String name=null; public static String number=null; //通知栏 private NotificationManager nm; //检测摇动相关变量 private long initTime = 0; private long lastTime = 0; private long curTime = 0; private long duration = 0; private float last_x = 0.0f; private float last_y = 0.0f; private float last_z = 0.0f; private float shake = 0.0f; private float totalShake = 0.0f; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { //设置主题 // setTheme(android.R.style.Theme_Holo); super.onCreate(savedInstanceState); setContentView(R.layout.main); //TTS部分 mEditText = (EditText)this.findViewById(R.id.edittext); readButton = (Button)this.findViewById(R.id.rbutton); saveButton = (Button)this.findViewById(R.id.sbutton); mCheckBox = (CheckBox)this.findViewById(R.id.checkbox); //speak部分 Button speakButton = (Button) findViewById(R.id.btn_speak); mList = (ListView) findViewById(R.id.list); sp_antext=(TextView)findViewById(R.id.text); // Check to see if a recognition activity is present //语音识别开始 PackageManager pm = getPackageManager(); List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size() != 0) { speakButton.setOnClickListener(i); //i监听事件在下面 } else { speakButton.setEnabled(false); speakButton.setText("Recognizer not present"); } //传感器运用 //实例并初始化TTS对象 mTextToSpeech = new TextToSpeech(this,new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { // TODO Auto-generated method stub if(status == TextToSpeech.SUCCESS) { //设置朗读语言,可设置英语或其他语言 int supported = mTextToSpeech.setLanguage(Locale.CHINESE); if((supported != TextToSpeech.LANG_AVAILABLE)&&(supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) { displayToast("不支持当前语言!"); } } } }); //朗读按钮监听 readButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //朗读EditText里的内容 mTextToSpeech.speak(mEditText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);

//朗读语音speaking内容 if(mCheckBox.isChecked()&& !matches.get(0).toString().isEmpty()){ mTextToSpeech.speak(matches.get(0).toString(),TextToSpeech.QUEUE_FLUSH, null); }else{ return; } } }); //保存按钮监听 saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //将EditText里的内容保存为语音文件 int r = mTextToSpeech.synthesizeToFile(mEditText.getText().toString(), null, "/mnt/sdcard/TTS_speak.wav"); if(r == TextToSpeech.SUCCESS) displayToast("保存成功!"); } }); //EditText内容变化监听 mEditText.addTextChangedListener(mTextWatcher); } private TextWatcher mTextWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub //如果是边写边读 if(mCheckBox.isChecked()&&(s.length()!=0)) { //获得EditText的所有内容 String t = s.toString(); mTextToSpeech.speak(t.substring(s.length()-1), TextToSpeech.QUEUE_FLUSH, null); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } }; //显示Toast函数 private void displayToast(String s) { Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } @Override public void onDestroy() { super.onDestroy(); if(mTextToSpeech != null) mTextToSpeech.shutdown();//关闭TTS } //speak部分 //speak按钮事件 OnClickListener i=new OnClickListener(){ public void onClick(View v) { if (v.getId() == R.id.btn_speak) { startVoiceRecognitionActivity(); } } }; /** * Fire an intent to start the speech recognition activity. */ private void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "请说出你的心声..."); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); } /** * Handle the results from the recognition activity. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { // Fill the list view with the strings the recognizer thought it could have heard // ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, matches)); //语音识别输出的语句 //将语音识别与朗读接通 String str_yuyin= matches.get(0).toString(); //获取系统时间,时间是int型,这里需用string.valueof(int):string转换 final Calendar c=Calendar.getInstance(); final String time_nian= String.valueOf(c.get(Calendar.YEAR)); final String time_yue=String.valueOf(c.get(Calendar.MONTH)); final String time_ri= String.valueOf(c.get(Calendar.DAY_OF_MONTH)); final String time_h= String.valueOf(c.get(Calendar.HOUR_OF_DAY)); final String time_m= String.valueOf(c.get(Calendar.MINUTE)); //尝试一下,即当语音识别结束后,语音不为空时实行 if(!str_yuyin.isEmpty()){ mTextToSpeech.speak(str_yuyin,TextToSpeech.QUEUE_FLUSH, null); //语音搜索与其识别后实行的事件 /* if(str_yuyin.equals("打电话给 10086")){ Uri uri=Uri.parse("tel:10086"); Intent inphone=new Intent(Intent.ACTION_CALL,uri); startActivity(inphone); */ if(str_yuyin.equals(name)){ Uri uri=Uri.parse("tel:number"); Intent inphone=new Intent(Intent.ACTION_CALL,uri); startActivity(inphone); /* 这是连接到打电话界面 Intent intent= new Intent("android.intent.action.DIAL"); intent.setClassName("com.android.contacts","com.android.contacts.DialtactsActivity"); startActivity(intent);*/ mTextToSpeech.speak("好的,亲爱的爸爸您放心,我现在就"+str_yuyin,TextToSpeech.QUEUE_FLUSH, null); } if(str_yuyin.equals(getString(R.string.sp_carma1))){ Intent int_carm = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); int_carm.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( Environment.getExternalStorageDirectory(), "temp.jpg"))); startActivityForResult(int_carm, PHOTOHRAPH); mTextToSpeech.speak(getString(R.string.an_carma1),TextToSpeech.QUEUE_FLUSH, null); } if(str_yuyin.equals(getString(R.string.sp_mulu2))){ //未完成 mTextToSpeech.speak(getString(R.string.an_mulu2),TextToSpeech.QUEUE_FLUSH, null); Intent intt =new Intent(Intent.ACTION_FACTORY_TEST); intt.setAction("android.intent.action.MAIN"); startActivity(intt); } if(str_yuyin.equals(getString(R.string.sp_xc3))){ //未完成 mTextToSpeech.speak(getString(R.string.an_xc3),TextToSpeech.QUEUE_FLUSH, null); Intent int_xiangc=new Intent(Intent.ACTION_GET_CONTENT); startActivity(int_xiangc); } if(str_yuyin.equals(getString(R.string.sp_sms4))){ mTextToSpeech.speak(getString(R.string.an_sms4),TextToSpeech.QUEUE_FLUSH, null); Intent int_sms=new Intent(Intent.ACTION_SEND); startActivity(int_sms); } if(str_yuyin.equals(getString(R.string.sp_soso5))){ mTextToSpeech.speak(getString(R.string.an_soso5),TextToSpeech.QUEUE_FLUSH, null); Intent int_web=new Intent(Intent.ACTION_WEB_SEARCH); startActivity(int_web); } if(str_yuyin.equals(getString(R.string.sp_mv6))){ mTextToSpeech.speak(getString(R.string.an_mv6),TextToSpeech.QUEUE_FLUSH, null); String path; Intent it = new Intent("com.cooliris.media.MovieView"); it.setAction(Intent.ACTION_VIEW); it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Uri datat = Uri.parse("file://sdcard/*"); it.setDataAndType(datat, "video/mp4"); startActivity(it); } if(str_yuyin.equals(getString(R.string.sp_em7))){ mTextToSpeech.speak(getString(R.string.an_em7),TextToSpeech.QUEUE_FLUSH, null); Uri uri_em = Uri.parse("mailto: "); Intent it_em = new Intent(Intent.ACTION_SENDTO, uri_em); startActivity(it_em); } if(str_yuyin.equals(getString(R.string.sp_map8))){ mTextToSpeech.speak(getString(R.string.an_map8),TextToSpeech.QUEUE_FLUSH, null); Uri uri_dtu = Uri.parse("geo:38.899533,-77.036476 "); Intent it_dtu = new Intent(Intent.ACTION_VIEW, uri_dtu); startActivity(it_dtu); } if(str_yuyin.equals(getString(R.string.sp_mak9))){ mTextToSpeech.speak(getString(R.string.an_mak9),TextToSpeech.QUEUE_FLUSH, null); Uri uri_yingy = Uri.parse("market://search?q=pname:pkg_name"); Intent it_yingy = new Intent(Intent.ACTION_VIEW, uri_yingy); startActivity(it_yingy); } if(str_yuyin.equals(getString(R.string.sp_qq10))){ mTextToSpeech.speak(getString(R.string.an_qq10),TextToSpeech.QUEUE_FLUSH, null); Intent int_qq=new Intent(); ComponentName comp=new ComponentName("com.tencent.mobileqq","com.tencent.mobileqq.activity.SplashActivity"); int_qq.setComponent(comp); int_qq.setAction("android.int_qq.action.MAIN"); int_qq.setFlags(int_qq.FLAG_ACTIVITY_CLEAR_TASK); startActivity(int_qq); } if(str_yuyin.equals(getString(R.string.sp_mm11))){ mTextToSpeech.speak(getString(R.string.an_mm11),TextToSpeech.QUEUE_FLUSH, null); Intent int_weixin = new Intent(); ComponentName weixin = new ComponentName("com.tencent.mm","com.tencent.mm.activity.SplashActivity"); int_weixin.setComponent(weixin); int_weixin.setAction("android.intent.action.MAIN"); startActivity(int_weixin); } if(str_yuyin.equals(getString(R.string.sp_mu12))||str_yuyin.equals(getString(R.string.sp_mu121))){ mTextToSpeech.speak(getString(R.string.an_mu12),TextToSpeech.QUEUE_FLUSH, null); Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri =Uri.parse("file:///extSdCard/song/.mp3"); intent.setDataAndType(uri,"audio/mp3"); startActivity(intent); } if(str_yuyin.equals(getString(R.string.sp_timed13))){ mTextToSpeech.speak("现在是"+time_h+"时"+time_m+"分,主人,如果你要忙什么现在就抓紧时间",TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText("现在是"+time_h+"时"+time_m+"分,主人,如果你要忙什么现在就抓紧时间"); } if(str_yuyin.equals(getString(R.string.sp_timeh14))){ mTextToSpeech.speak("主人,今天是"+time_nian+"年"+time_yue+"月"+time_ri+"号,哈哈,是不是觉得时间过得快,要珍惜美好时光",TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText("主人,今天是"+time_nian+"年"+time_yue+"月"+time_ri+"号,哈哈,是不是觉得时间过得快,要珍惜美好时光"); //待验证 } //语音聊天 if(str_yuyin.equals(getString(R.string.sp_name15))||str_yuyin.equals(getString(R.string.sp_name151))||str_yuyin.equals(getString(R.string.sp_name152))){ mTextToSpeech.speak(getString(R.string.an_name15),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_name15); } if(str_yuyin.equals(getString(R.string.sp_fth16))||str_yuyin.equals(getString(R.string.sp_fth161))||str_yuyin.equals(getString(R.string.sp_fth162))){ mTextToSpeech.speak(getString(R.string.an_fth16),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_fth16); } if(str_yuyin.equals(getString(R.string.sp_ntime17))){ mTextToSpeech.speak(getString(R.string.an_ntime17),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_ntime17); } if(str_yuyin.equals(getString(R.string.sp_helpme18))){ mTextToSpeech.speak(getString(R.string.an_helpme18),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_helpme18); } if(str_yuyin.equals(getString(R.string.sp_ygood19))){ mTextToSpeech.speak(getString(R.string.an_ygood19),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_ygood19); } if(str_yuyin.equals(getString(R.string.sp_xiaoh20))||str_yuyin.equals(getString(R.string.sp_xiaoh201))){ mTextToSpeech.speak(getString(R.string.an_xiaoh20),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_xiaoh20); } if(str_yuyin.equals(getString(R.string.sp_ll21))||str_yuyin.equals(getString(R.string.sp_ll211))){ mTextToSpeech.speak(getString(R.string.an_ll21),TextToSpeech.QUEUE_FLUSH, null); sp_antext.setText(R.string.an_ll21); } if(str_yuyin.equals(getString(R.string.sp_tianqi22))||str_yuyin.equals(getString(R.string.sp_tianqi221))){ mTextToSpeech.speak(getString(R.string.an_tianqi22),TextToSpeech.QUEUE_FLUSH, null);

你可能感兴趣的:(TTS与google语言之别)