How to execute both Speech Recognition and Audio recording at the same time?
<p><strong>THIS IS NOT A DUPLICATE, HERE I TALK ABOUT STOPPING AND STARTING THE RECORD PROCESS WHENEVER I WANT. BEFORE MARKING AS DUPLICATES, PLEASE READ THE OTHER ANSWER PROPERLY.</strong></p>

<p>I am developing a <code>Phonegap</code> plugin for <code>Android</code>. This plugin will basically support the <code>Android Speech Recognition</code> and recording the speech. I am capable of starting, stopping etc the speech recognition, but got serious issues with recording. First, the code is posted below.</p>

<p>Below is how I start the speech, end it etc.</p>

<pre><code>    public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException
    {

        final int duration = Toast.LENGTH_SHORT;
        this.callBackContext = callbackContext;

        // Shows a toast
        Log.v(TAG,"CoolPlugin received:"+ action);

        if(INITIALIZE.equals(action))
        {
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    init();


                }
                });
        }
        else if(PLAY.equals(action))
        {
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    //startRecording();
                    play(callbackContext);
                    timeMilli = System.currentTimeMillis();
                    timer();
                    AudioRecorder recorder = AudioRecorder.getInstance();
                    recorder.startRecording();
                }
                });
        }
        else if(STOP.equals(action))
        {
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    stop();
                    AudioRecorder recorder = AudioRecorder.getInstance();
                    recorder.stopRecording();               

                }
                });
        }

        return true;
    }

    public void play(CallbackContext callbackContext)
    {
        //Mute the sound
        mAudioManager = (AudioManager) cordova.getActivity().getSystemService(Context.AUDIO_SERVICE) ;
        mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); // getting system volume into var for later un-muting
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // setting system volume to

        //Start listening
        speech.stopListening();
        speech.cancel();
        speech.destroy();
        createRecog();
        speech.startListening(recognizerIntent);
        //adapter.clear();
    }

public  String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }

    return message;
}
</code></pre>

<p>Below is my code for Recording the speech. It is a separated class.</p>

<pre><code>import java.io.FileDescriptor;
import java.io.IOException;
import java.io.File;

import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

public class AudioRecorder {

     private static final String LOG_TAG = "AudioRecoder";
    private MediaRecorder mRecorder;
    String file;
    public static AudioRecorder instance;

    public static AudioRecorder getInstance()
    {
        if(instance==null)
        {
            instance = new AudioRecorder();
        }
        return instance;
    }

    private AudioRecorder()
    {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
        File dir = new File(path);
        if(!dir.exists())
            dir.mkdirs();
    //  String myfile = path + "filename" + ".mp4";

        file=path + "filename2" + ".3gp";
    }

    public void startRecording() {

        new Thread(new Runnable(){

            @Override
            public void run() {
                 mRecorder = new MediaRecorder();
                    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mRecorder.setOutputFile(file);
                    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

                    try {
                        mRecorder.prepare();
                        Log.e(LOG_TAG, "prepare() success");
                    } catch (IOException e) {
                       // Log.e(LOG_TAG, "prepare() failed");
                        Log.d(LOG_TAG, "prepare() failed", e);
                    }

                    mRecorder.start();
                    Log.e(LOG_TAG, "Working");

            }}).start();

        }

    public void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }


}
</code></pre>

<p>Now, there is an issue. When starting the recognition service, if I didn't use the below code, then speech recognition is working fine.</p>

<pre><code>AudioRecorder recorder = AudioRecorder.getInstance();
recorder.startRecording();
</code></pre>

<p>However if I use the above code, then I get the following error</p>

<pre><code>02-03 15:33:21.555: D/VoiceRecognitionActivity(32066): FAILED Network error
02-03 15:33:21.575: E/SpeechRecognizer(32066): not connected to the recognition service
02-03 15:33:21.575: E/SpeechRecognizer(32066): not connected to the recognition service
</code></pre>

<p>Basically speaking, if I execute the recording process, then the recognition process is giving errors. What is happening here please?</p>

<p>In the recording process I will have to make functions where the user can start the recording at anytime he wants and stop it at anytime he likes.</p>