Java tutorial
package com.ppdl.microphone; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.SharedPreferences; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.speech.RecognitionListener; import android.speech.SpeechRecognizer; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.baidu.speech.VoiceRecognitionService; public class MainActivity extends Activity { public static final String TAG = "Microphone"; MediaRecorder mMediaRecorder; MediaPlayer mMediaPlayer; String mMediaFilePath; private SpeechRecognizer speechRecognizer; private long speechEndTime = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); sp.edit().remove(Constant.EXTRA_INFILE).commit(); // infile?PCM????? Button recordButton = (Button) findViewById(R.id.buttonRecord); recordButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startRecording(); Toast.makeText(MainActivity.this, "Start Recording", Toast.LENGTH_SHORT).show(); break; case MotionEvent.ACTION_UP: stopRecording(); v.performClick(); Toast.makeText(MainActivity.this, "Stop Recording", Toast.LENGTH_SHORT).show(); break; default: break; } return false; } }); Button playButton = (Button) findViewById(R.id.buttonPlay); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startPlaying(); } }); Button stopButton = (Button) findViewById(R.id.buttonStop); stopButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopPlaying(); } }); Button recognizeButton = (Button) findViewById(R.id.buttonRecognize); recognizeButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Toast.makeText(MainActivity.this, "Start Recognizing", Toast.LENGTH_SHORT).show(); speechRecognizer.cancel(); Intent intent = new Intent(); bindParams(intent); intent.putExtra("vad", "touch"); speechRecognizer.startListening(intent); break; case MotionEvent.ACTION_UP: v.performClick(); Toast.makeText(MainActivity.this, "Stop Recognizing", Toast.LENGTH_SHORT).show(); speechRecognizer.stopListening(); break; default: break; } return false; } }); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class)); speechRecognizer.setRecognitionListener(mRecognitionListner); } @Override public void onDestroy() { speechRecognizer.destroy(); super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void startRecording() { mMediaRecorder = new MediaRecorder(); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); mMediaRecorder.setOutputFile(newRecordName()); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mMediaRecorder.prepare(); mMediaRecorder.start(); } catch (Exception e) { e.printStackTrace(); } } public void stopRecording() { if (mMediaRecorder != null) { mMediaRecorder.stop(); mMediaRecorder.reset(); mMediaRecorder.release(); mMediaRecorder = null; } } public void startPlaying() { File file = new File(mMediaFilePath); if (file.exists()) { Log.i(TAG, "file is: " + file.getAbsolutePath()); mMediaPlayer = new MediaPlayer(); try { mMediaPlayer.setDataSource(mMediaFilePath); mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.i(TAG, "<<<<<<<<<<<< end >>>>>>>>>>>>>>"); stopPlaying(); } }); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (Exception e) { e.printStackTrace(); } } } public void stopPlaying() { if (mMediaPlayer != null) { mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.release(); mMediaPlayer = null; } } private String newRecordName() { String fileName = Environment.getExternalStorageDirectory().getAbsolutePath(); String dateName = new SimpleDateFormat("yyyy-mm-dd hhmmss").format(new Date()); fileName += "/rcd" + dateName + ".amr"; Log.i(TAG, "filepath: " + fileName); mMediaFilePath = fileName; return fileName; } RecognitionListener mRecognitionListner = new RecognitionListener() { @Override public void onReadyForSpeech(Bundle params) { Log.i(TAG, "onReadyForSpeech"); } @Override public void onBeginningOfSpeech() { Log.i(MainActivity.TAG, "onBeginningOfSpeech"); } @Override public void onRmsChanged(float rmsdB) { Log.i(MainActivity.TAG, "onRmsChanged"); } @Override public void onBufferReceived(byte[] buffer) { Log.i(MainActivity.TAG, "onBufferReceived"); } @Override public void onEndOfSpeech() { Log.i(MainActivity.TAG, "onEndOfSpeech"); } @Override public void onError(int error) { Log.i(MainActivity.TAG, "onError: " + error); 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); Toast.makeText(MainActivity.this, "" + sb.toString(), Toast.LENGTH_SHORT).show(); } @Override public void onResults(Bundle results) { Log.i(MainActivity.TAG, "onResults"); ArrayList<String> nbest = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); TextView tvRet = (TextView) findViewById(R.id.textViewResult); tvRet.setText("" + Arrays.toString(nbest.toArray(new String[nbest.size()]))); } @Override public void onPartialResults(Bundle partialResults) { Log.i(MainActivity.TAG, "onPartialResults"); ArrayList<String> nbest = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (nbest.size() > 0) { Toast.makeText(MainActivity.this, "~" + Arrays.toString(nbest.toArray(new String[0])), Toast.LENGTH_SHORT).show(); } } @Override public void onEvent(int eventType, Bundle params) { Log.i(MainActivity.TAG, "onEvent"); final int EVENT_ERROR = 11; switch (eventType) { case EVENT_ERROR: String reason = params.get("reason") + ""; Log.i(MainActivity.TAG, "Error: " + reason); break; } } }; 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); } } String prop = null; if (sp.contains(Constant.EXTRA_PROP)) { String tmp = sp.getString(Constant.EXTRA_PROP, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { Log.i(TAG, "=======$====" + tmp); intent.putExtra(Constant.EXTRA_PROP, Integer.parseInt(tmp)); prop = tmp; } } // offline asr { intent.putExtra("sample", 16000); // ? 16000 intent.putExtra("language", "cmn-Hans-CN"); // ?? intent.putExtra("prop", 20000); Log.i(TAG, "============= " + Environment.getExternalStorageDirectory().getPath()); intent.putExtra(Constant.EXTRA_OFFLINE_ASR_BASE_FILE_PATH, Environment.getExternalStorageDirectory().getPath() + "/s_1"); intent.putExtra(Constant.EXTRA_LICENSE_FILE_PATH, Environment.getExternalStorageDirectory().getPath() + "/temp_license_2016-04-18"); ///if (null != prop) { // int propInt = Integer.parseInt(prop); // if (propInt == 10060) { intent.putExtra(Constant.EXTRA_OFFLINE_LM_RES_FILE_PATH, Environment.getExternalStorageDirectory().getPath() + "/s_2_Navi"); //// } else if (propInt == 20000) { intent.putExtra(Constant.EXTRA_OFFLINE_LM_RES_FILE_PATH, Environment.getExternalStorageDirectory().getPath() + "/s_2_InputMethod"); // } //} intent.putExtra(Constant.EXTRA_OFFLINE_SLOT_DATA, buildTestSlotData()); } } private String buildTestSlotData() { JSONObject slotData = new JSONObject(); JSONArray name = new JSONArray().put("?").put(""); JSONArray song = new JSONArray().put("").put("?"); JSONArray artist = new JSONArray().put("?").put("?"); JSONArray app = new JSONArray().put("").put(""); JSONArray usercommand = new JSONArray().put("?").put(""); try { slotData.put("name", name); slotData.put("song", song); slotData.put("artist", artist); slotData.put("app", app); slotData.put("usercommand", usercommand); } catch (JSONException e) { e.printStackTrace(); } Log.i(TAG, name.toString() + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); Log.i(TAG, slotData.toString() + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); return slotData.toString(); } }