I am currently trying to record audio the user is saying so that they can play it back at a later time as well as use the Google Speech to Text API, so that I can run analysis on the words the API returns. It seems as though the microphone can either work exclusively with Google Speech to Text, or it can record. I can do both separately, but when I try to run them together on my Android app, the app will stop the Speech to Text and will just start the audio recording. I am running the Audio record in a background thread, while running the speech to text in the main UI thread. It looks something like this right now:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// set up Speech to Text Recognizer
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(this);
//Set up Recording functionality
bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
buffer = new byte[bufferSize];
//Set up Record Button
record = (ToggleButton) findViewById(R.id.record);
record.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){ //record
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak!");
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.example");
mSpeechRecognizer.startListening(i);
// end speech to text
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, 2048);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
saveAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();