eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eSpeakActivity.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (C) 2012-2013 Reece H. Dunn
  3. * Copyright (C) 2009 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.reecedunn.espeak;
  18. import android.app.Activity;
  19. import android.content.Intent;
  20. import android.os.Build;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.preference.PreferenceActivity;
  25. import android.speech.tts.TextToSpeech;
  26. import android.util.Log;
  27. import android.util.Pair;
  28. import android.view.Menu;
  29. import android.view.MenuInflater;
  30. import android.view.MenuItem;
  31. import android.view.View;
  32. import android.widget.ListView;
  33. import android.widget.EditText;
  34. import java.lang.ref.WeakReference;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Locale;
  38. public class eSpeakActivity extends Activity {
  39. private static final String ACTION_TTS_SETTINGS = "com.android.settings.TTS_SETTINGS";
  40. /** Handler code for TTS initialization hand-off. */
  41. private static final int TTS_INITIALIZED = 1;
  42. private static final int REQUEST_CHECK = 1;
  43. private static final int REQUEST_DEFAULT = 3;
  44. private static final String TAG = "eSpeakActivity";
  45. private enum State {
  46. LOADING,
  47. DOWNLOAD_FAILED,
  48. ERROR,
  49. SUCCESS
  50. }
  51. private State mState;
  52. private ArrayList<String> mVoices;
  53. private TextToSpeech mTts;
  54. private List<Pair<String,String>> mInformation;
  55. private InformationListAdapter mInformationView;
  56. private EditText mText;
  57. @Override
  58. public void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.main);
  61. mInformation = new ArrayList<Pair<String,String>>();
  62. mInformationView = new InformationListAdapter(this, mInformation);
  63. ((ListView)findViewById(R.id.properties)).setAdapter(mInformationView);
  64. mText = (EditText)findViewById(R.id.editText1);
  65. setState(State.LOADING);
  66. checkVoiceData();
  67. findViewById(R.id.speak).setOnClickListener(new View.OnClickListener() {
  68. @Override
  69. public void onClick(View v) {
  70. mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
  71. }
  72. });
  73. findViewById(R.id.ssml).setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View v) {
  76. String ssml =
  77. "<?xml version=\"1.0\"?>\n" +
  78. "<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" version=\"1.0\">\n" +
  79. "\n" +
  80. "</speak>";
  81. mText.setText(ssml);
  82. }
  83. });
  84. }
  85. @Override
  86. public void onStop() {
  87. super.onStop();
  88. if (mTts != null) {
  89. mTts.shutdown();
  90. }
  91. }
  92. @Override
  93. public boolean onCreateOptionsMenu(Menu menu)
  94. {
  95. MenuInflater inflater = getMenuInflater();
  96. inflater.inflate(R.menu.options, menu);
  97. if (Build.VERSION.SDK_INT < 14) {
  98. // Hide the eSpeak setting menu item on pre-ICS.
  99. menu.findItem(R.id.espeakSettings).setVisible(false);
  100. }
  101. return true;
  102. }
  103. @Override
  104. public boolean onOptionsItemSelected(MenuItem item)
  105. {
  106. switch (item.getItemId())
  107. {
  108. case R.id.espeakSettings:
  109. startActivityForResult(new Intent(eSpeakActivity.this, TtsSettingsActivity.class), REQUEST_DEFAULT);
  110. return true;
  111. case R.id.ttsSettings:
  112. launchGeneralTtsSettings();
  113. return true;
  114. }
  115. return super.onOptionsItemSelected(item);
  116. }
  117. /**
  118. * Sets the UI state.
  119. *
  120. * @param state The current state.
  121. */
  122. private void setState(State state) {
  123. mState = state;
  124. switch (mState)
  125. {
  126. case LOADING:
  127. findViewById(R.id.loading).setVisibility(View.VISIBLE);
  128. findViewById(R.id.success).setVisibility(View.GONE);
  129. break;
  130. default:
  131. findViewById(R.id.loading).setVisibility(View.GONE);
  132. findViewById(R.id.success).setVisibility(View.VISIBLE);
  133. break;
  134. }
  135. }
  136. /**
  137. * Launcher the voice data verifier.
  138. */
  139. private void checkVoiceData() {
  140. final Intent checkIntent = new Intent(this, CheckVoiceData.class);
  141. startActivityForResult(checkIntent, REQUEST_CHECK);
  142. }
  143. /**
  144. * Initializes the TTS engine.
  145. */
  146. private void initializeEngine() {
  147. mTts = new TextToSpeech(this, mInitListener);
  148. }
  149. private void populateInformationView() {
  150. mInformation.clear();
  151. if (mTts != null) {
  152. Locale language = mTts.getLanguage();
  153. if (language != null) {
  154. final String currentLocale = getString(R.string.current_tts_locale);
  155. mInformation.add(new Pair<String,String>(currentLocale, mTts.getLanguage().getDisplayName()));
  156. }
  157. }
  158. final String availableVoices = getString(R.string.available_voices);
  159. if (mVoices == null) {
  160. mInformation.add(new Pair<String,String>(availableVoices, "0"));
  161. } else {
  162. mInformation.add(new Pair<String,String>(availableVoices, Integer.toString(mVoices.size())));
  163. }
  164. final String version = getString(R.string.tts_version);
  165. mInformation.add(new Pair<String,String>(version, SpeechSynthesis.getVersion()));
  166. final String statusText;
  167. switch (mState) {
  168. case ERROR:
  169. statusText = getString(R.string.error_message);
  170. break;
  171. case DOWNLOAD_FAILED:
  172. statusText = getString(R.string.voice_data_failed_message);
  173. break;
  174. default:
  175. if (!getPackageName().equals(mTts.getDefaultEngine())) {
  176. statusText = getString(R.string.set_default_message);
  177. } else {
  178. statusText = null;
  179. }
  180. break;
  181. }
  182. if (statusText != null) {
  183. final String statusLabel = getString(R.string.status);
  184. mInformation.add(new Pair<String,String>(statusLabel, statusText));
  185. }
  186. mInformationView.notifyDataSetChanged();
  187. }
  188. /**
  189. * Handles the result of voice data verification. If verification fails
  190. * following a successful installation, displays an error dialog. Otherwise,
  191. * either launches the installer or attempts to initialize the TTS engine.
  192. *
  193. * @param resultCode The result of voice data verification.
  194. * @param data The intent containing available voices.
  195. */
  196. private void onDataChecked(int resultCode, Intent data) {
  197. if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  198. Log.e(TAG, "Voice data check failed (error code: " + resultCode + ").");
  199. setState(State.ERROR);
  200. } else {
  201. mVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
  202. }
  203. initializeEngine();
  204. populateInformationView();
  205. }
  206. /**
  207. * Handles the result of TTS engine initialization. Either displays an error
  208. * dialog or populates the activity's UI.
  209. *
  210. * @param status The TTS engine initialization status.
  211. */
  212. private void onInitialized(int status) {
  213. if (status != TextToSpeech.SUCCESS) {
  214. Log.e(TAG, "Initialization failed (status: " + status + ").");
  215. setState(State.ERROR);
  216. } else {
  217. setState(State.SUCCESS);
  218. }
  219. populateInformationView();
  220. }
  221. @Override
  222. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  223. switch (requestCode) {
  224. case REQUEST_CHECK:
  225. onDataChecked(resultCode, data);
  226. break;
  227. case REQUEST_DEFAULT:
  228. initializeEngine();
  229. break;
  230. }
  231. super.onActivityResult(requestCode, resultCode, data);
  232. }
  233. private final TextToSpeech.OnInitListener mInitListener = new TextToSpeech.OnInitListener() {
  234. @Override
  235. public void onInit(int status) {
  236. mHandler.obtainMessage(TTS_INITIALIZED, status, 0).sendToTarget();
  237. }
  238. };
  239. private static class EspeakHandler extends Handler {
  240. private WeakReference<eSpeakActivity> mActivity;
  241. public EspeakHandler(eSpeakActivity activity)
  242. {
  243. mActivity = new WeakReference<eSpeakActivity>(activity);
  244. }
  245. @Override
  246. public void handleMessage(Message msg) {
  247. switch (msg.what) {
  248. case TTS_INITIALIZED:
  249. mActivity.get().onInitialized(msg.arg1);
  250. break;
  251. }
  252. }
  253. }
  254. private final Handler mHandler = new EspeakHandler(this);
  255. private void launchGeneralTtsSettings()
  256. {
  257. Intent intent;
  258. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  259. {
  260. // The Text-to-Speech settings is a Fragment on 3.x:
  261. intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
  262. intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, "com.android.settings.TextToSpeechSettings");
  263. intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, intent.getExtras());
  264. }
  265. else
  266. {
  267. // The Text-to-Speech settings is an Activity on 2.x and 4.x:
  268. intent = new Intent(ACTION_TTS_SETTINGS);
  269. }
  270. startActivityForResult(intent, REQUEST_DEFAULT);
  271. }
  272. }