Google cloud speech pause

822 Views Asked by At

I am having a problem with Google Cloud Speech. I cannot pause Google Cloud Speech, to pause the recognition I am currently shutting down the SpeechRecognizer, and when i need it again, I am starting it again. But this way, The Stopping takes much time to stop, and i have to run in background thread. Some times it stops right in time, but not always. For few days i used in on Main Thread i have not got any problems with it but now it started to skip frames so i moved it. I need it instant stop because after stop is followed by Speech to text, and if i wait till the recognition is stopped the the flow will look laggy even if i moved it on background thread. I am using the code from their sample, to start and stop it :

    private void stopVoiceRecorder() {
    mNotifyForVoiceEnd.start();
    if (mVoiceRecorder != null) {
        mVoiceRecorder.stop();
    }
    isListening = false;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            appBar.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.primary));

        }
    });
}

Starting the Recognition:

 private void startVoiceRecorder() {
    mNotifyForVoiceStart.start();
    if (mVoiceRecorder != null) {
        mVoiceRecorder.stop();
        mVoiceRecorder = null;
    }
    mVoiceRecorder = new VoiceRecorder(mVoiceCallback);
    mVoiceRecorder.start();
    isListening = true;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            appBar.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.tab_not_hearing));
        }
    });
}

I used TraceView and the problem doing much work is

mVoiceRecorder.stop();

and the code to .stop()

   public void stop() {
    synchronized (mLock) {
        if (mThread != null) {
            mThread.interrupt();
            mThread = null;
        }

        dismiss();
        if (mAudioRecord != null) {
            mAudioRecord.stop();
        }

    }
}

Is there any way to pause the recognition?

1

There are 1 best solutions below

0
On BEST ANSWER

I found the solution :

 // Simply delete   synchronized (mLock)

   public void stop() {

    if (mThread != null) {
        mThread.interrupt();
        mThread = null;
    }

    dismiss();
    if (mAudioRecord != null) {
        mAudioRecord.stop();
    }

}