Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8b7bc669 authored by Android (Google) Code Review's avatar Android (Google) Code Review Committed by The Android Open Source Project
Browse files

am b45451f6: Merge change 6583 into donut

Merge commit 'b45451f6'

* commit 'b45451f6':
  Adding a lock to stop so that stop will stop utterances
parents a5f451f3 b45451f6
Loading
Loading
Loading
Loading
+36 −11
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.TimeUnit;


/**
 * @hide Synthesizes speech from text. This is implemented as a service so that
@@ -358,10 +360,17 @@ public class TtsService extends Service implements OnCompletionListener {
     * Stops all speech output and removes any utterances still in the queue.
     */
    private int stop() {
        int result = TextToSpeech.TTS_ERROR;
        boolean speechQueueAvailable = false;
        try{
            // If the queue is locked for more than 1 second,
            // something has gone very wrong with processSpeechQueue.
            speechQueueAvailable = speechQueueLock.tryLock(1000, TimeUnit.MILLISECONDS);
            if (speechQueueAvailable) {
                Log.i("TTS", "Stopping");
                mSpeechQueue.clear();

        int result = nativeSynth.stop();
                result = nativeSynth.stop();
                mIsSpeaking = false;
                if (mPlayer != null) {
                    try {
@@ -371,8 +380,19 @@ public class TtsService extends Service implements OnCompletionListener {
                    }
                }
                Log.i("TTS", "Stopped");
            }
        } catch (InterruptedException e) {
          Log.e("TTS stop", "tryLock interrupted");
          e.printStackTrace();
        } finally {
            // This check is needed because finally will always run; even if the
            // method returns somewhere in the try block.
            if (speechQueueAvailable) {
                speechQueueLock.unlock();
            }
            return result;
        }
    }

    public void onCompletion(MediaPlayer arg0) {
        processSpeechQueue();
@@ -443,6 +463,7 @@ public class TtsService extends Service implements OnCompletionListener {
                    }
                    nativeSynth.speak(text);
                } catch (InterruptedException e) {
                    Log.e("TTS speakInternalOnly", "tryLock interrupted");
                    e.printStackTrace();
                } finally {
                    // This check is needed because finally will always run;
@@ -497,6 +518,7 @@ public class TtsService extends Service implements OnCompletionListener {
                    }
                    nativeSynth.synthesizeToFile(text, filename);
                } catch (InterruptedException e) {
                    Log.e("TTS synthToFileInternalOnly", "tryLock interrupted");
                    e.printStackTrace();
                } finally {
                    // This check is needed because finally will always run;
@@ -650,6 +672,9 @@ public class TtsService extends Service implements OnCompletionListener {
            if (mSpeechQueue.size() > 0) {
                mSpeechQueue.remove(0);
            }
        } catch (InterruptedException e) {
            Log.e("TTS processSpeechQueue", "tryLock interrupted");
            e.printStackTrace();
        } finally {
            // This check is needed because finally will always run; even if the
            // method returns somewhere in the try block.