Loading packages/TtsService/src/android/tts/TtsService.java +36 −11 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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 { Loading @@ -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(); Loading Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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. Loading Loading
packages/TtsService/src/android/tts/TtsService.java +36 −11 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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 { Loading @@ -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(); Loading Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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. Loading