1 MainActivity 语音转文字
package com.sammer.xfyun; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; 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.SpeechUtility; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.LinkedHashMap; public class MainActivity extends AppCompatActivity { private TextView tv_content; private HashMap2 文字转语音 Main2ActivitymIatResults = new LinkedHashMap (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content = (TextView) findViewById(R.id.textView_content); SpeechUtility.createUtility(MainActivity.this, SpeechConstant.APPID + "=........."); // 这里要替换成自己申请的 AppID } public void startSpeechClick(View view){ //1.创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener SpeechRecognizer mIat= SpeechRecognizer.createRecognizer(this, null); //2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类 mIat.setParameter(SpeechConstant.DOMAIN, "iat");//应用领域 mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");//中文 mIat.setParameter(SpeechConstant.ACCENT, "mandarin");//普通话 // 设置听写引擎 mIat.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 设置返回结果格式 mIat.setParameter(SpeechConstant.RESULT_TYPE, "json"); // 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理 mIat.setParameter(SpeechConstant.VAD_BOS, "4000"); // 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音 mIat.setParameter(SpeechConstant.VAD_EOS, "1000"); // 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点 mIat.setParameter(SpeechConstant.ASR_PTT, "1"); //3.开始听写 mIat.startListening(mRecoListener); } //听写监听器 private RecognizerListener mRecoListener = new RecognizerListener() { @Override //音量 public void onVolumeChanged(int i, byte[] bytes) { } //开始录音 @Override public void onBeginOfSpeech() { } //结束录音 @Override public void onEndOfSpeech() { } //返回结果 @Override public void onResult(RecognizerResult recognizerResult, boolean b) { System.out.println(recognizerResult.getResultString()); printResult(recognizerResult); } @Override public void onError(SpeechError speechError) { } @Override public void onEvent(int i, int i1, int i2, Bundle bundle) { } }; private void printResult(RecognizerResult results) { String text = JsonParser.parseIatResult(results.getResultString()); String sn = null; // 读取json结果中的sn字段 try { JSONObject resultJson = new JSONObject(results.getResultString()); sn = resultJson.optString("sn"); } catch (JSONException e) { e.printStackTrace(); } mIatResults.put(sn, text); StringBuffer resultBuffer = new StringBuffer(); for (String key : mIatResults.keySet()) { resultBuffer.append(mIatResults.get(key)); } tv_content.setText(resultBuffer.toString()); } }
package com.sammer.xfyun; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechSynthesizer; import com.iflytek.cloud.SpeechUtility; import com.iflytek.cloud.SynthesizerListener; public class Main2Activity extends AppCompatActivity { private EditText editText_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); editText_content =(EditText) findViewById(R.id.editText_content); SpeechUtility.createUtility(this, SpeechConstant.APPID + "=..........."); // 这里要替换成自己申请的 AppID } public void startClick(View view){ // //1.创建SpeechSynthesizer对象,第二个参数:本地听写时传InitListener SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null); //设置云端 mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 设置在线合成发音人 mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); //设置合成语速 mTts.setParameter(SpeechConstant.SPEED,"50"); //设置合成音调 // mTts.setParameter(SpeechConstant.PITCH, "50"); //设置合成音量 mTts.setParameter(SpeechConstant.VOLUME, "80"); String content = editText_content.getText().toString(); mTts.startSpeaking(content, mTtsListener); } //合成回调监听。 private SynthesizerListener mTtsListener = new SynthesizerListener(){ @Override public void onSpeakBegin() { } @Override public void onBufferProgress(int i, int i1, int i2, String s) { } @Override public void onSpeakPaused() { } @Override public void onSpeakResumed() { } @Override public void onSpeakProgress(int i, int i1, int i2) { } @Override public void onCompleted(SpeechError speechError) { } @Override public void onEvent(int i, int i1, int i2, Bundle bundle) { } }; }