編輯:關於Android編程
import java.util.List; import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.EngineInfo; import android.speech.tts.TextToSpeech.OnInitListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import android.widget.AdapterView.OnItemSelectedListener; public class TTSActivity extends Activity implements OnClickListener { private TextToSpeech mSpeech; private EditText et_tts_resource; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tts); et_tts_resource = (EditText) findViewById(R.id.et_tts_resource); Button btn_tts_start = (Button) findViewById(R.id.btn_tts_start); btn_tts_start.setOnClickListener(this); initLanguageSpinner(); mSpeech = new TextToSpeech(TTSActivity.this, new TTSListener()); } private void initLanguageSpinner() { ArrayAdapterstarAdapter = new ArrayAdapter (this, R.layout.spinner_item, mLangArray); starAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); Spinner sp = (Spinner) findViewById(R.id.sp_tts_language); sp.setPrompt("請選擇語言"); sp.setAdapter(starAdapter); sp.setOnItemSelectedListener(new LanguageSelectedListener()); sp.setSelection(0); } private String[] mEngineArray; private int mEngine; private void initEngineSpinner() { mEngineArray = new String[mEngineList.size()]; for(int i=0; i starAdapter = new ArrayAdapter (this, R.layout.spinner_item, mEngineArray); starAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); Spinner sp = (Spinner) findViewById(R.id.sp_tts_engine); sp.setPrompt("請選擇引擎"); sp.setAdapter(starAdapter); sp.setOnItemSelectedListener(new EngineSelectedListener()); sp.setSelection(0); } @Override protected void onDestroy() { recycleSpeech(); super.onDestroy(); } private void recycleSpeech() { if (mSpeech != null) { mSpeech.stop(); mSpeech.shutdown(); mSpeech = null; } } private String[] mLangArray = {"英語", "法語", "德語", "意大利語", "漢語普通話" }; private Locale[] mLocaleArray = { Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, Locale.ITALIAN, Locale.CHINA }; private int mLanguage; private String mTextEN = "hello world. This is a TTS demo."; private String mTextCN = "白日依山盡,黃河入海流。欲窮千裡目,更上一層樓。"; private class LanguageSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { mLanguage = arg2; if (mLocaleArray[mLanguage]==Locale.SIMPLIFIED_CHINESE || mLocaleArray[mLanguage]==Locale.TRADITIONAL_CHINESE) { et_tts_resource.setText(mTextCN); } else { et_tts_resource.setText(mTextEN); } if (mEngineList != null) { resetLanguage(); } } public void onNothingSelected(AdapterView arg0) { } } private class EngineSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { mEngine = arg2; recycleSpeech(); mSpeech = new TextToSpeech(TTSActivity.this, new TTSListener(), mEngineList.get(mEngine).name); } public void onNothingSelected(AdapterView arg0) { } } private void resetLanguage() { int result = mSpeech.setLanguage(mLocaleArray[mLanguage]); //如果打印為-2,說明不支持這種語言;-1說明缺失數據 Toast.makeText(TTSActivity.this, "您選擇的是"+mLangArray[mLanguage] +",result="+result, Toast.LENGTH_SHORT).show(); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { } } @Override public void onClick(View v) { if (v.getId() == R.id.btn_tts_start) { String content = et_tts_resource.getText().toString(); int result = mSpeech.speak(content, TextToSpeech.QUEUE_FLUSH, null); Toast.makeText(TTSActivity.this, "speak result="+result, Toast.LENGTH_SHORT).show(); } } private List mEngineList; private class TTSListener implements OnInitListener { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { if (mEngineList == null) { mEngineList = mSpeech.getEngines(); initEngineSpinner(); } else { resetLanguage(); } } } } }
SpeechUtility.createUtility(MainApplication.this, "appid=5763c4cf");4、在AndroidManifest.xml中加入必要的權限,以及自定義的Application類;
import java.util.HashMap; import java.util.LinkedHashMap; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; import com.example.exmvoice.R; import com.example.exmvoice.SettingsActivity; import com.example.exmvoice.xunfei.util.FucUtil; import com.example.exmvoice.xunfei.util.JsonParser; 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.ui.RecognizerDialog; import com.iflytek.cloud.ui.RecognizerDialogListener; public class XFRecognizeActivity extends Activity implements OnClickListener { private final static String TAG = XFRecognizeActivity.class.getSimpleName(); // 語音聽寫對象 private SpeechRecognizer mRecognize; // 語音聽寫UI private RecognizerDialog mRecognizeDialog; // 用HashMap存儲聽寫結果 private HashMapmRecognizeResults = new LinkedHashMap (); private EditText mResultText; private SharedPreferences mSharedPreferences; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_xunfei_recognize); mResultText = ((EditText) findViewById(R.id.xf_recognize_text)); findViewById(R.id.xf_recognize_start).setOnClickListener(this); findViewById(R.id.xf_recognize_stop).setOnClickListener(this); findViewById(R.id.xf_recognize_cancel).setOnClickListener(this); findViewById(R.id.xf_recognize_stream).setOnClickListener(this); findViewById(R.id.xf_recognize_setting).setOnClickListener(this); mSharedPreferences = getSharedPreferences(SettingsActivity.PREFER_NAME, Activity.MODE_PRIVATE); // 初始化識別無UI識別對象,使用SpeechRecognizer對象,可根據回調消息自定義界面; mRecognize = SpeechRecognizer.createRecognizer(this, mInitListener); // 初始化聽寫Dialog,如果只使用有UI聽寫功能,無需創建SpeechRecognizer // 使用UI聽寫功能,請將assets下文件拷貝到項目中 mRecognizeDialog = new RecognizerDialog(this, mInitListener); } @Override protected void onDestroy() { super.onDestroy(); // 退出時釋放連接 mRecognize.cancel(); mRecognize.destroy(); } @Override public void onClick(View v) { int ret = 0; // 函數調用返回值 int resid = v.getId(); if (resid == R.id.xf_recognize_setting) { // 進入參數設置頁面 Intent intent = new Intent(this, SettingsActivity.class); intent.putExtra("type", SettingsActivity.XF_RECOGNIZE); startActivity(intent); } else if (resid == R.id.xf_recognize_start) { // 開始聽寫。如何判斷一次聽寫結束:OnResult isLast=true 或者 onError mResultText.setText(null);// 清空顯示內容 mRecognizeResults.clear(); // 設置參數 resetParam(); boolean isShowDialog = mSharedPreferences.getBoolean("show_dialog", true); if (isShowDialog) { // 顯示聽寫對話框 mRecognizeDialog.setListener(mRecognizeDialogListener); mRecognizeDialog.show(); showTip("請開始說話………"); } else { // 不顯示聽寫對話框 ret = mRecognize.startListening(mRecognizeListener); if (ret != ErrorCode.SUCCESS) { showTip("聽寫失敗,錯誤碼:" + ret); } else { showTip("請開始說話…"); } } } else if (resid == R.id.xf_recognize_stop) { // 停止聽寫 mRecognize.stopListening(); showTip("停止聽寫"); } else if (resid == R.id.xf_recognize_cancel) { // 取消聽寫 mRecognize.cancel(); showTip("取消聽寫"); } else if (resid == R.id.xf_recognize_stream) { // 音頻流識別 mResultText.setText(null);// 清空顯示內容 mRecognizeResults.clear(); // 設置參數 resetParam(); // 設置音頻來源為外部文件 mRecognize.setParameter(SpeechConstant.AUDIO_SOURCE, "-1"); // 也可以像以下這樣直接設置音頻文件路徑識別(要求設置文件在sdcard上的全路徑): // mRecognize.setParameter(SpeechConstant.AUDIO_SOURCE, "-2"); // mRecognize.setParameter(SpeechConstant.ASR_SOURCE_PATH, "sdcard/XXX/XXX.pcm"); ret = mRecognize.startListening(mRecognizeListener); if (ret != ErrorCode.SUCCESS) { showTip("識別失敗,錯誤碼:" + ret); } else { byte[] audioData = FucUtil.readAudioFile(this, "retcognize_est.wav"); if (null != audioData) { showTip("開始音頻流識別"); // 一次(也可以分多次)寫入音頻文件數據,數據格式必須是采樣率為8KHz或16KHz(本地識別只支持16K采樣率,雲端都支持),位長16bit,單聲道的wav或者pcm // 寫入8KHz采樣的音頻時,必須先調用setParameter(SpeechConstant.SAMPLE_RATE, "8000")設置正確的采樣率 // 注:當音頻過長,靜音部分時長超過VAD_EOS將導致靜音後面部分不能識別 mRecognize.writeAudio(audioData, 0, audioData.length); mRecognize.stopListening(); } else { mRecognize.cancel(); showTip("讀取音頻流失敗"); } } } } //初始化監聽器 private InitListener mInitListener = new InitListener() { @Override public void onInit(int code) { Log.d(TAG, "SpeechRecognizer init() code = " + code); if (code != ErrorCode.SUCCESS) { showTip("初始化失敗,錯誤碼:" + code); } } }; //聽寫監聽器 private RecognizerListener mRecognizeListener = new RecognizerListener() { @Override public void onBeginOfSpeech() { // 此回調表示:sdk內部錄音機已經准備好了,用戶可以開始語音輸入 showTip("開始說話"); } @Override public void onError(SpeechError error) { // 錯誤碼:10118(您沒有說話),可能是錄音機權限被禁,需要提示用戶打開應用的錄音權限。 // 如果使用本地功能(語記)需要提示用戶開啟語記的錄音權限。 showTip(error.getPlainDescription(true)); } @Override public void onEndOfSpeech() { // 此回調表示:檢測到了語音的尾端點,已經進入識別過程,不再接受語音輸入 showTip("結束說話"); } @Override public void onResult(RecognizerResult results, boolean isLast) { Log.d(TAG, results.getResultString()); printResult(results); if (isLast) { // TODO 最後的結果 } } @Override public void onVolumeChanged(int volume, byte[] data) { showTip("當前正在說話,音量大小:" + volume); Log.d(TAG, "返回音頻數據:"+data.length); } @Override public void onEvent(int eventType, int arg1, int arg2, Bundle obj) { // 以下代碼用於獲取與雲端的會話id,當業務出錯時將會話id提供給技術支持人員,可用於查詢會話日志,定位出錯原因 // 若使用本地能力,會話id為null // if (SpeechEvent.EVENT_SESSION_ID == eventType) { // String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID); // Log.d(TAG, "session id =" + sid); // } } }; private void printResult(RecognizerResult results) { String text = JsonParser.parseIatResult(results.getResultString()); String sn = null; try { JSONObject resultJson = new JSONObject(results.getResultString()); sn = resultJson.optString("sn"); } catch (JSONException e) { e.printStackTrace(); return; } mRecognizeResults.put(sn, text); StringBuffer resultBuffer = new StringBuffer(); for (String key : mRecognizeResults.keySet()) { resultBuffer.append(mRecognizeResults.get(key)); } mResultText.setText(resultBuffer.toString()); mResultText.setSelection(mResultText.length()); } //聽寫UI監聽器 private RecognizerDialogListener mRecognizeDialogListener = new RecognizerDialogListener() { public void onResult(RecognizerResult results, boolean isLast) { printResult(results); } //識別回調錯誤 public void onError(SpeechError error) { showTip(error.getPlainDescription(true)); } }; private void showTip(final String str) { Toast.makeText(this, str, Toast.LENGTH_LONG).show(); } //參數設置 public void resetParam() { // 清空參數 mRecognize.setParameter(SpeechConstant.PARAMS, null); // 設置聽寫引擎。TYPE_LOCAL表示本地,TYPE_CLOUD表示雲端,TYPE_MIX 表示混合 mRecognize.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 設置返回結果格式 mRecognize.setParameter(SpeechConstant.RESULT_TYPE, "json"); String lag = mSharedPreferences.getString("recognize_language_preference", "mandarin"); if (lag.equals("en_us")) { // 設置語言 mRecognize.setParameter(SpeechConstant.LANGUAGE, "en_us"); } else { mRecognize.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); // 設置語言區域 mRecognize.setParameter(SpeechConstant.ACCENT, lag); } // 設置語音前端點:靜音超時時間,即用戶多長時間不說話則當做超時處理 mRecognize.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("recognize_vadbos_preference", "4000")); // 設置語音後端點:後端點靜音檢測時間,即用戶停止說話多長時間內即認為不再輸入, 自動停止錄音 mRecognize.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("recognize_vadeos_preference", "1000")); // 設置標點符號,設置為"0"返回結果無標點,設置為"1"返回結果有標點 mRecognize.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("recognize_punc_preference", "1")); // 設置音頻保存路徑,保存音頻格式支持pcm、wav,設置路徑為sd卡請注意WRITE_EXTERNAL_STORAGE權限 // 注:AUDIO_FORMAT參數語記需要更新版本才能生效 mRecognize.setParameter(SpeechConstant.AUDIO_FORMAT, "wav"); mRecognize.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/recognize.wav"); } }
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; import com.example.exmvoice.R; import com.example.exmvoice.SettingsActivity; import com.iflytek.cloud.ErrorCode; import com.iflytek.cloud.InitListener; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechSynthesizer; import com.iflytek.cloud.SynthesizerListener; public class XFComposeActivity extends Activity implements OnClickListener { private static String TAG = XFComposeActivity.class.getSimpleName(); // 語音合成對象 private SpeechSynthesizer mCompose; // 默認發音人 private String voicer = "xiaoyan"; private String[] mCloudVoicersEntries; private String[] mCloudVoicersValue ; // 緩沖進度 private int mPercentForBuffering = 0; // 播放進度 private int mPercentForPlaying = 0; private EditText mResourceText; private SharedPreferences mSharedPreferences; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_xunfei_compose); mResourceText = ((EditText) findViewById(R.id.xf_compose_text)); findViewById(R.id.xf_compose_play).setOnClickListener(this); findViewById(R.id.xf_compose_cancel).setOnClickListener(this); findViewById(R.id.xf_compose_pause).setOnClickListener(this); findViewById(R.id.xf_compose_resume).setOnClickListener(this); findViewById(R.id.xf_compose_setting).setOnClickListener(this); findViewById(R.id.xf_compose_person).setOnClickListener(this); mSharedPreferences = getSharedPreferences(SettingsActivity.PREFER_NAME, MODE_PRIVATE); // 初始化合成對象 mCompose = SpeechSynthesizer.createSynthesizer(this, mComposeInitListener); // 雲端發音人名稱列表 mCloudVoicersEntries = getResources().getStringArray(R.array.voicer_cloud_entries); mCloudVoicersValue = getResources().getStringArray(R.array.voicer_cloud_values); } @Override protected void onDestroy() { super.onDestroy(); // 退出時釋放連接 mCompose.stopSpeaking(); mCompose.destroy(); } @Override public void onClick(View v) { int resid = v.getId(); if (resid == R.id.xf_compose_setting) { Intent intent = new Intent(this, SettingsActivity.class); intent.putExtra("type", SettingsActivity.XF_COMPOSE); startActivity(intent); } else if (resid == R.id.xf_compose_play) { // 開始合成 //收到onCompleted 回調時,合成結束、生成合成音頻。合成的音頻格式:只支持pcm格式 String text = mResourceText.getText().toString(); // 設置參數 setParam(); int code = mCompose.startSpeaking(text, mComposeListener); if (code != ErrorCode.SUCCESS) { showTip("語音合成失敗,錯誤碼: " + code); } // //只保存音頻不進行播放接口,調用此接口請注釋startSpeaking接口 // //text:要合成的文本,uri:需要保存的音頻全路徑,listener:回調接口 // String path = Environment.getExternalStorageDirectory()+"/compose.pcm"; // int code = mCompose.synthesizeToUri(text, path, mComposeListener); } else if (resid == R.id.xf_compose_cancel) { // 取消合成 mCompose.stopSpeaking(); } else if (resid == R.id.xf_compose_pause) { // 暫停播放 mCompose.pauseSpeaking(); } else if (resid == R.id.xf_compose_resume) { // 繼續播放 mCompose.resumeSpeaking(); } else if (resid == R.id.xf_compose_person) { // 選擇發音人 showPresonSelectDialog(); } } private int selectedNum = 0; //發音人選擇 private void showPresonSelectDialog() { new AlertDialog.Builder(this).setTitle("在線合成發音人選項") .setSingleChoiceItems(mCloudVoicersEntries, // 單選框有幾項,各是什麼名字 selectedNum, // 默認的選項 new DialogInterface.OnClickListener() { // 點擊單選框後的處理 public void onClick(DialogInterface dialog, int which) { // 點擊了哪一項 voicer = mCloudVoicersValue[which]; if ("catherine".equals(voicer) || "henry".equals(voicer) || "vimary".equals(voicer) || "Mariane".equals(voicer) || "Allabent".equals(voicer) || "Gabriela".equals(voicer) || "Abha".equals(voicer) || "XiaoYun".equals(voicer)) { mResourceText.setText(R.string.compose_source_en); } else { mResourceText.setText(R.string.compose_source); } selectedNum = which; dialog.dismiss(); } }).show(); } //初始化監聽 private InitListener mComposeInitListener = new InitListener() { @Override public void onInit(int code) { Log.d(TAG, "InitListener init() code = " + code); if (code != ErrorCode.SUCCESS) { showTip("初始化失敗,錯誤碼:"+code); } else { // 初始化成功,之後可以調用startSpeaking方法 // 注:有的開發者在onCreate方法中創建完合成對象之後馬上就調用startSpeaking進行合成, // 正確的做法是將onCreate中的startSpeaking調用移至這裡 } } }; //合成回調監聽 private SynthesizerListener mComposeListener = new SynthesizerListener() { @Override public void onSpeakBegin() { showTip("開始播放"); } @Override public void onSpeakPaused() { showTip("暫停播放"); } @Override public void onSpeakResumed() { showTip("繼續播放"); } @Override public void onBufferProgress(int percent, int beginPos, int endPos, String info) { // 合成進度 mPercentForBuffering = percent; showTip(String.format(getString(R.string.xf_compose_toast_format), mPercentForBuffering, mPercentForPlaying)); } @Override public void onSpeakProgress(int percent, int beginPos, int endPos) { // 播放進度 mPercentForPlaying = percent; showTip(String.format(getString(R.string.xf_compose_toast_format), mPercentForBuffering, mPercentForPlaying)); } @Override public void onCompleted(SpeechError error) { if (error == null) { showTip("播放完成"); } else if (error != null) { showTip(error.getPlainDescription(true)); } } @Override public void onEvent(int eventType, int arg1, int arg2, Bundle obj) { // 以下代碼用於獲取與雲端的會話id,當業務出錯時將會話id提供給技術支持人員,可用於查詢會話日志,定位出錯原因 // 若使用本地能力,會話id為null // if (SpeechEvent.EVENT_SESSION_ID == eventType) { // String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID); // Log.d(TAG, "session id =" + sid); // } } }; private void showTip(final String str) { Toast.makeText(this, str, Toast.LENGTH_LONG).show(); } //參數設置 private void setParam(){ // 清空參數 mCompose.setParameter(SpeechConstant.PARAMS, null); // 根據合成引擎設置相應參數 mCompose.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 設置在線合成發音人 mCompose.setParameter(SpeechConstant.VOICE_NAME, voicer); //設置合成語速 mCompose.setParameter(SpeechConstant.SPEED, mSharedPreferences.getString("speed_preference", "50")); //設置合成音調 mCompose.setParameter(SpeechConstant.PITCH, mSharedPreferences.getString("pitch_preference", "50")); //設置合成音量 mCompose.setParameter(SpeechConstant.VOLUME, mSharedPreferences.getString("volume_preference", "50")); //設置播放器音頻流類型 mCompose.setParameter(SpeechConstant.STREAM_TYPE, mSharedPreferences.getString("stream_preference", "3")); // 設置播放合成音頻打斷音樂播放,默認為true mCompose.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true"); // 設置音頻保存路徑,保存音頻格式支持pcm、wav,設置路徑為sd卡請注意WRITE_EXTERNAL_STORAGE權限 // 注:AUDIO_FORMAT參數語記需要更新版本才能生效 mCompose.setParameter(SpeechConstant.AUDIO_FORMAT, "wav"); mCompose.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/compose.wav"); } }
import com.example.exmvoice.R; import com.example.exmvoice.SettingsActivity; import com.example.exmvoice.xunfei.util.SettingTextWatcher; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceFragment; //語音識別設置界面 public class XFRecognizeSettingsFragment extends PreferenceFragment implements OnPreferenceChangeListener { private EditTextPreference mVadbosPreference; private EditTextPreference mVadeosPreference; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getPreferenceManager().setSharedPreferencesName(SettingsActivity.PREFER_NAME); addPreferencesFromResource(R.xml.xf_recognize_setting); mVadbosPreference = (EditTextPreference) findPreference("recognize_vadbos_preference"); mVadbosPreference.getEditText().addTextChangedListener( new SettingTextWatcher(getActivity(),mVadbosPreference,0,10000)); mVadeosPreference = (EditTextPreference) findPreference("recognize_vadeos_preference"); mVadeosPreference.getEditText().addTextChangedListener( new SettingTextWatcher(getActivity(),mVadeosPreference,0,10000)); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { return true; } }
4、demo工程中assets目錄下的文件原樣拷過來;
import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.speech.RecognitionListener; import android.speech.SpeechRecognizer; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; import com.baidu.speech.VoiceRecognitionService; import com.example.exmvoice.R; import com.example.exmvoice.SettingsActivity; import com.example.exmvoice.baidu.setting.Constant; import org.json.JSONObject; import java.util.*; public class BDRecognizeActivity extends Activity implements OnClickListener { private static final String TAG = BDRecognizeActivity.class.getSimpleName(); private static final int REQUEST_UI = 1; private TextView txtResult; private TextView txtLog; private Button btnStart; public static final int STATUS_None = 0; public static final int STATUS_WaitingReady = 2; public static final int STATUS_Ready = 3; public static final int STATUS_Speaking = 4; public static final int STATUS_Recognition = 5; private SpeechRecognizer speechRecognizer; private int status = STATUS_None; private long speechEndTime = -1; private static final int EVENT_ERROR = 11; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baidu_recognize); txtResult = (TextView) findViewById(R.id.bd_recognize_text); txtLog = (TextView) findViewById(R.id.bd_recognize_log); btnStart = (Button) findViewById(R.id.bd_recognize_start); btnStart.setOnClickListener(this); findViewById(R.id.bd_recognize_setting).setOnClickListener(this); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class)); speechRecognizer.setRecognitionListener(mRecognitionListener); } @Override protected void onDestroy() { speechRecognizer.destroy(); super.onDestroy(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { mRecognitionListener.onResults(data.getExtras()); } else { status = STATUS_None; btnStart.setText("開始"); } } public void bindParams(Intent intent) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); if (sp.getBoolean("tips_sound", true)) { intent.putExtra(Constant.EXTRA_SOUND_START, R.raw.bdspeech_recognition_start); intent.putExtra(Constant.EXTRA_SOUND_END, R.raw.bdspeech_speech_end); intent.putExtra(Constant.EXTRA_SOUND_SUCCESS, R.raw.bdspeech_recognition_success); intent.putExtra(Constant.EXTRA_SOUND_ERROR, R.raw.bdspeech_recognition_error); intent.putExtra(Constant.EXTRA_SOUND_CANCEL, R.raw.bdspeech_recognition_cancel); } if (sp.contains(Constant.EXTRA_INFILE)) { String tmp = sp.getString(Constant.EXTRA_INFILE, "").replaceAll(",.*", "").trim(); intent.putExtra(Constant.EXTRA_INFILE, tmp); } if (sp.getBoolean(Constant.EXTRA_OUTFILE, false)) { intent.putExtra(Constant.EXTRA_OUTFILE, "sdcard/outfile.pcm"); } if (sp.contains(Constant.EXTRA_SAMPLE)) { String tmp = sp.getString(Constant.EXTRA_SAMPLE, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_SAMPLE, Integer.parseInt(tmp)); } } if (sp.contains(Constant.EXTRA_LANGUAGE)) { String tmp = sp.getString(Constant.EXTRA_LANGUAGE, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_LANGUAGE, tmp); } } if (sp.contains(Constant.EXTRA_NLU)) { String tmp = sp.getString(Constant.EXTRA_NLU, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_NLU, tmp); } } if (sp.contains(Constant.EXTRA_VAD)) { String tmp = sp.getString(Constant.EXTRA_VAD, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_VAD, tmp); } } if (sp.contains(Constant.EXTRA_PROP)) { String tmp = sp.getString(Constant.EXTRA_PROP, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_PROP, Integer.parseInt(tmp)); } } } private void start() { btnStart.setText("取消"); txtLog.setText(""); status = STATUS_WaitingReady; print("點擊了“開始”"); Intent intent = new Intent(); bindParams(intent); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); { String args = sp.getString("args", ""); if (null != args) { print("參數集:" + args); intent.putExtra("args", args); } } boolean api = sp.getBoolean("api", false); if (api) { speechEndTime = -1; speechRecognizer.startListening(intent); } else { intent.setAction("com.baidu.action.RECOGNIZE_SPEECH"); startActivityForResult(intent, REQUEST_UI); } txtResult.setText(""); } private void stop() { speechRecognizer.stopListening(); status = STATUS_Recognition; btnStart.setText("識別中"); print("點擊了“說完了”"); } private void cancel() { speechRecognizer.cancel(); btnStart.setText("開始"); status = STATUS_None; print("點擊了“取消”"); } private RecognitionListener mRecognitionListener = new RecognitionListener() { @Override public void onReadyForSpeech(Bundle params) { status = STATUS_Ready; print("准備就緒,可以開始說話"); } @Override public void onBeginningOfSpeech() { status = STATUS_Speaking; btnStart.setText("說完了"); print("檢測到用戶已經開始說話"); } @Override public void onRmsChanged(float rmsdB) { } @Override public void onBufferReceived(byte[] buffer) { } @Override public void onEndOfSpeech() { speechEndTime = System.currentTimeMillis(); status = STATUS_Recognition; print("檢測到用戶已經停止說話"); btnStart.setText("識別中"); } @Override public void onError(int error) { status = STATUS_None; StringBuilder sb = new StringBuilder(); switch (error) { case SpeechRecognizer.ERROR_AUDIO: sb.append("音頻問題"); break; case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: sb.append("沒有語音輸入"); break; case SpeechRecognizer.ERROR_CLIENT: sb.append("其它客戶端錯誤"); break; case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: sb.append("權限不足"); break; case SpeechRecognizer.ERROR_NETWORK: sb.append("網絡問題"); break; case SpeechRecognizer.ERROR_NO_MATCH: sb.append("沒有匹配的識別結果"); break; case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: sb.append("引擎忙"); break; case SpeechRecognizer.ERROR_SERVER: sb.append("服務端錯誤"); break; case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: sb.append("連接超時"); break; } sb.append(":" + error); print("識別失敗:" + sb.toString()); btnStart.setText("開始"); } @Override public void onResults(Bundle results) { long end2finish = System.currentTimeMillis() - speechEndTime; ArrayListnbest = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); print("識別成功:" + Arrays.toString(nbest.toArray(new String[nbest.size()]))); String json_res = results.getString("origin_result"); try { print("origin_result=\n" + new JSONObject(json_res).toString(4)); } catch (Exception e) { print("origin_result=[warning: bad json]\n" + json_res); } String strEnd2Finish = ""; if (end2finish < 60 * 1000) { strEnd2Finish = "(waited " + end2finish + "ms)"; } txtResult.setText(nbest.get(0) + strEnd2Finish); status = STATUS_None; btnStart.setText("開始"); } @Override public void onPartialResults(Bundle partialResults) { ArrayList nbest = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (nbest.size() > 0) { print("~臨時識別結果:" + Arrays.toString(nbest.toArray(new String[0]))); txtResult.setText(nbest.get(0)); } } @Override public void onEvent(int eventType, Bundle params) { switch (eventType) { case EVENT_ERROR: String reason = params.get("reason") + ""; print("EVENT_ERROR, " + reason); status = STATUS_None; btnStart.setText("開始"); break; case VoiceRecognitionService.EVENT_ENGINE_SWITCH: int type = params.getInt("engine_type"); print("*引擎切換至" + (type == 0 ? "在線" : "離線")); break; } } }; private void print(String msg) { txtLog.append(msg + "\n"); ScrollView sv = (ScrollView) txtLog.getParent(); sv.smoothScrollTo(0, 1000000); Log.d(TAG, "----" + msg); } @Override public void onClick(View v) { int resid = v.getId(); if (resid == R.id.bd_recognize_setting) { Intent intent = new Intent(this, SettingsActivity.class); intent.putExtra("type", SettingsActivity.BD_RECOGNIZE); startActivity(intent); } else if (resid == R.id.bd_recognize_start) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); boolean api = sp.getBoolean("api", false); if (api) { if (status == STATUS_None) { start(); } else if (status == STATUS_WaitingReady || status == STATUS_Ready || status == STATUS_Recognition) { cancel(); } else if (status == STATUS_Speaking) { stop(); } } else { start(); } } } }
import java.io.File; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.media.AudioManager; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; import com.baidu.tts.auth.AuthInfo; import com.baidu.tts.client.SpeechError; import com.baidu.tts.client.SpeechSynthesizer; import com.baidu.tts.client.SpeechSynthesizerListener; import com.baidu.tts.client.TtsMode; import com.example.exmvoice.R; import com.example.exmvoice.SettingsActivity; import com.example.exmvoice.baidu.util.AssetsUtil; public class BDComposeActivity extends Activity implements OnClickListener,OnCheckedChangeListener { private static String TAG = BDComposeActivity.class.getSimpleName(); private SpeechSynthesizer mSpeechSynthesizer; private String mSampleDirPath; private static final String SAMPLE_DIR_NAME = "baiduTTS"; private static final String SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female.dat"; private static final String SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male.dat"; private static final String TEXT_MODEL_NAME = "bd_etts_text.dat"; private static final String LICENSE_FILE_NAME = "bd_temp_license"; private static final String ENGLISH_SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female_en.dat"; private static final String ENGLISH_SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male_en.dat"; private static final String ENGLISH_TEXT_MODEL_NAME = "bd_etts_text_en.dat"; private boolean bOnline = true; private EditText mResourceText; private SharedPreferences mSharedPreferences; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_baidu_compose); mResourceText = ((EditText) findViewById(R.id.bd_compose_text)); ((RadioGroup)findViewById(R.id.bd_compose_mode)).setOnCheckedChangeListener(this); findViewById(R.id.bd_compose_play).setOnClickListener(this); findViewById(R.id.bd_compose_cancel).setOnClickListener(this); findViewById(R.id.bd_compose_pause).setOnClickListener(this); findViewById(R.id.bd_compose_resume).setOnClickListener(this); findViewById(R.id.bd_compose_setting).setOnClickListener(this); mSharedPreferences = getSharedPreferences(SettingsActivity.PREFER_NAME, MODE_PRIVATE); initialEnv(); initialEngine(); } private void initialEnv() { if (mSampleDirPath == null) { String sdcardPath = Environment.getExternalStorageDirectory().toString(); mSampleDirPath = sdcardPath + "/" + SAMPLE_DIR_NAME; } File file = new File(mSampleDirPath); if (!file.exists()) { file.mkdirs(); } AssetsUtil.copyFromAssetsToSdcard(this, false, SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME); AssetsUtil.copyFromAssetsToSdcard(this, false, SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_MALE_MODEL_NAME); AssetsUtil.copyFromAssetsToSdcard(this, false, TEXT_MODEL_NAME, mSampleDirPath + "/" + TEXT_MODEL_NAME); AssetsUtil.copyFromAssetsToSdcard(this, false, LICENSE_FILE_NAME, mSampleDirPath + "/" + LICENSE_FILE_NAME); AssetsUtil.copyFromAssetsToSdcard(this, false, "english/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME); AssetsUtil.copyFromAssetsToSdcard(this, false, "english/" + ENGLISH_SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + ENGLISH_SPEECH_MALE_MODEL_NAME); AssetsUtil.copyFromAssetsToSdcard(this, false, "english/" + ENGLISH_TEXT_MODEL_NAME, mSampleDirPath + "/" + ENGLISH_TEXT_MODEL_NAME); } private void initialEngine() { mSpeechSynthesizer = SpeechSynthesizer.getInstance(); mSpeechSynthesizer.setContext(this); mSpeechSynthesizer.setSpeechSynthesizerListener(mSpeechListener); ApplicationInfo appInfo = null; try { appInfo = this.getPackageManager().getApplicationInfo( getPackageName(), PackageManager.GET_META_DATA); String app_id = appInfo.metaData.getString("com.baidu.speech.APP_ID"); String api_key = appInfo.metaData.getString("com.baidu.speech.API_KEY"); String secret_key = appInfo.metaData.getString("com.baidu.speech.SECRET_KEY"); mSpeechSynthesizer.setAppId(app_id); mSpeechSynthesizer.setApiKey(api_key, secret_key); } catch (NameNotFoundException e) { e.printStackTrace(); showTip("獲取appid失敗"); } AuthInfo authInfo = mSpeechSynthesizer.auth(TtsMode.ONLINE); if (authInfo.isSuccess()) { showTip("auth success"); } else { String errorMsg = authInfo.getTtsError().getDetailMessage(); showTip("auth failed errorMsg=" + errorMsg); } mSpeechSynthesizer.initTts(TtsMode.MIX); bOnline = ((RadioButton) findViewById(R.id.bd_compose_online)).isChecked(); setParams(bOnline); } @Override protected void onDestroy() { // 退出時釋放連接 mSpeechSynthesizer.release(); super.onDestroy(); } @Override public void onClick(View v) { int resid = v.getId(); if (resid == R.id.bd_compose_setting) { Intent intent = new Intent(this, SettingsActivity.class); intent.putExtra("type", SettingsActivity.BD_COMPOSE); startActivity(intent); } else if (resid == R.id.bd_compose_play) { // 開始合成 speak(); } else if (resid == R.id.bd_compose_cancel) { // 取消合成 mSpeechSynthesizer.stop(); } else if (resid == R.id.bd_compose_pause) { // 暫停播放 mSpeechSynthesizer.pause(); } else if (resid == R.id.bd_compose_resume) { // 繼續播放 mSpeechSynthesizer.resume(); } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.bd_compose_online) { bOnline = true; } else if (checkedId == R.id.bd_compose_offline) { bOnline = false; } Log.d(TAG, "bOnline="+bOnline); setParams(bOnline); } private void setParams(boolean online) { mSpeechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC); //setVolumeControlStream(AudioManager.STREAM_MUSIC); mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, mSharedPreferences.getString("bd_person_preference", "0")); //0--普通女聲,1--普通男聲,2--特別男聲,3--情感男聲 mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, mSharedPreferences.getString("bd_volume_preference", "5")); //音量,取值0-9,默認為5中音量 mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, mSharedPreferences.getString("bd_speed_preference", "5")); //語速,取值0-9,默認為5中語速 mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, mSharedPreferences.getString("bd_pitch_preference", "5")); //音調,取值0-9,默認為5中語調 mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, SpeechSynthesizer.AUDIO_ENCODE_AMR); mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, SpeechSynthesizer.AUDIO_BITRATE_AMR_15K85); if (online == true) { } else { // 文本模型文件路徑 (離線引擎使用) mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, mSampleDirPath + "/" + TEXT_MODEL_NAME); // 聲學模型文件路徑 (離線引擎使用) mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME); // 本地授權文件路徑,如未設置將使用默認路徑.設置臨時授權文件路徑,LICENCE_FILE_NAME請替換成臨時授權文件的實際路徑,僅在使用臨時license文件時需要進行設置,如果在[應用管理]中開通了離線授權,不需要設置該參數,建議將該行代碼刪除(離線引擎) mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_LICENCE_FILE, mSampleDirPath + "/" + LICENSE_FILE_NAME); // 設置Mix模式的合成策略 mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_DEFAULT); // 加載離線英文資源(提供離線英文合成功能) String englishTextPath = mSampleDirPath + "/" + ENGLISH_TEXT_MODEL_NAME; String englishSpeechPath = mSampleDirPath + "/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME; Log.d(TAG, "englishTextPath="+englishTextPath+", englishSpeechPath="+englishSpeechPath); int result = mSpeechSynthesizer.loadEnglishModel(englishTextPath, englishSpeechPath); showTip("loadEnglishModel result=" + result); //如果initTts使用的是在線模式TtsMode.ONLINE,則loadEnglishModel會失敗返回-11 } } private void speak() { final String text = mResourceText.getText().toString(); if (text==null || text.length()<=0) { showTip("請輸入要合成語音的文字"); } else { int result = mSpeechSynthesizer.speak(text); if (result < 0) { showTip("result="+result+". error,please look up error code in doc or URL:http://yuyin.baidu.com/docs/tts/122 "); } else { showTip("合成結果="+result); } } } private SpeechSynthesizerListener mSpeechListener = new SpeechSynthesizerListener() { @Override public void onSynthesizeStart(String utteranceId) { toPrint("onSynthesizeStart utteranceId=" + utteranceId); } @Override public void onSynthesizeDataArrived(String utteranceId, byte[] data, int progress) { // toPrint("onSynthesizeDataArrived"); } @Override public void onSynthesizeFinish(String utteranceId) { toPrint("onSynthesizeFinish utteranceId=" + utteranceId); } @Override public void onSpeechStart(String utteranceId) { toPrint("onSpeechStart utteranceId=" + utteranceId); } @Override public void onSpeechProgressChanged(String utteranceId, int progress) { // toPrint("onSpeechProgressChanged"); } @Override public void onSpeechFinish(String utteranceId) { toPrint("onSpeechFinish utteranceId=" + utteranceId); } @Override public void onError(String utteranceId, SpeechError error) { toPrint("onError error=" + "(" + error.code + ")" + error.description + "--utteranceId=" + utteranceId); } }; private void showTip(final String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); String message = (String) msg.obj; if (message != null) { Log.d(TAG, message); showTip(message); } } }; private void toPrint(String str) { Message msg = Message.obtain(); msg.obj = str; this.mHandler.sendMessage(msg); } }
pendingIntent字面意義:等待的,未決定的Intent。要得到一個pendingIntent對象,使用方法類的靜態方法getActivity(Context,
今天在調ViewPager的時候,感覺ViewPager+Fragment這種做法更靈活,所以,現在拿出來Fragment再整理下。 一,為什麼要用
手機qq聊天記錄裡有重要的信息要怎麼導出?手機qq聊天記錄保存在哪個文件夾,我們一起來看看吧!手機qq導出聊天記錄方法:一、導出與好友的聊天記錄: 操作方
最近在弄個項目,要求有跑馬燈效果的圖片展示。網上搜了一堆,都沒有完美實現的算了還是自己寫吧! 實現原理利用 ViewPager 控件,這個控件本身就支持滑動翻頁很好很強大