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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_DOWNLOAD = 2;
  44. private static final int REQUEST_DEFAULT = 3;
  45. private static final String TAG = "eSpeakActivity";
  46. private enum State {
  47. LOADING,
  48. DOWNLOAD_FAILED,
  49. ERROR,
  50. SUCCESS
  51. }
  52. private State mState;
  53. private boolean mDownloadedVoiceData;
  54. private ArrayList<String> mVoices;
  55. private TextToSpeech mTts;
  56. private List<Pair<String,String>> mInformation;
  57. private InformationListAdapter mInformationView;
  58. private EditText mText;
  59. @Override
  60. public void onCreate(Bundle savedInstanceState) {
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.main);
  63. mInformation = new ArrayList<Pair<String,String>>();
  64. mInformationView = new InformationListAdapter(this, mInformation);
  65. ((ListView)findViewById(R.id.properties)).setAdapter(mInformationView);
  66. mText = (EditText)findViewById(R.id.editText1);
  67. setState(State.LOADING);
  68. checkVoiceData();
  69. findViewById(R.id.speak).setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View v) {
  72. mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
  73. }
  74. });
  75. findViewById(R.id.ssml).setOnClickListener(new View.OnClickListener() {
  76. @Override
  77. public void onClick(View v) {
  78. String ssml =
  79. "<?xml version=\"1.0\"?>\n" +
  80. "<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" version=\"1.0\">\n" +
  81. "\n" +
  82. "</speak>";
  83. mText.setText(ssml);
  84. }
  85. });
  86. }
  87. @Override
  88. public void onStop() {
  89. super.onStop();
  90. if (mTts != null) {
  91. mTts.shutdown();
  92. }
  93. }
  94. @Override
  95. public boolean onCreateOptionsMenu(Menu menu)
  96. {
  97. MenuInflater inflater = getMenuInflater();
  98. inflater.inflate(R.menu.options, menu);
  99. if (Build.VERSION.SDK_INT < 14) {
  100. // Hide the eSpeak setting menu item on pre-ICS.
  101. menu.findItem(R.id.espeakSettings).setVisible(false);
  102. }
  103. return true;
  104. }
  105. @Override
  106. public boolean onOptionsItemSelected(MenuItem item)
  107. {
  108. switch (item.getItemId())
  109. {
  110. case R.id.espeakSettings:
  111. startActivityForResult(new Intent(eSpeakActivity.this, TtsSettingsActivity.class), REQUEST_DEFAULT);
  112. return true;
  113. case R.id.ttsSettings:
  114. launchGeneralTtsSettings();
  115. return true;
  116. }
  117. return super.onOptionsItemSelected(item);
  118. }
  119. /**
  120. * Sets the UI state.
  121. *
  122. * @param state The current state.
  123. */
  124. private void setState(State state) {
  125. mState = state;
  126. switch (mState)
  127. {
  128. case LOADING:
  129. findViewById(R.id.loading).setVisibility(View.VISIBLE);
  130. findViewById(R.id.success).setVisibility(View.GONE);
  131. break;
  132. default:
  133. findViewById(R.id.loading).setVisibility(View.GONE);
  134. findViewById(R.id.success).setVisibility(View.VISIBLE);
  135. break;
  136. }
  137. }
  138. /**
  139. * Launcher the voice data verifier.
  140. */
  141. private void checkVoiceData() {
  142. final Intent checkIntent = new Intent(this, CheckVoiceData.class);
  143. startActivityForResult(checkIntent, REQUEST_CHECK);
  144. }
  145. /**
  146. * Launches the voice data installer.
  147. */
  148. private void downloadVoiceData() {
  149. final Intent checkIntent = new Intent(this, DownloadVoiceData.class);
  150. startActivityForResult(checkIntent, REQUEST_DOWNLOAD);
  151. }
  152. /**
  153. * Initializes the TTS engine.
  154. */
  155. private void initializeEngine() {
  156. mTts = new TextToSpeech(this, mInitListener);
  157. }
  158. private void populateInformationView() {
  159. mInformation.clear();
  160. if (mTts != null) {
  161. Locale language = mTts.getLanguage();
  162. if (language != null) {
  163. final String currentLocale = getString(R.string.current_tts_locale);
  164. mInformation.add(new Pair<String,String>(currentLocale, mTts.getLanguage().getDisplayName()));
  165. }
  166. }
  167. final String availableVoices = getString(R.string.available_voices);
  168. if (mVoices == null) {
  169. mInformation.add(new Pair<String,String>(availableVoices, "0"));
  170. } else {
  171. mInformation.add(new Pair<String,String>(availableVoices, Integer.toString(mVoices.size())));
  172. }
  173. final String version = getString(R.string.tts_version);
  174. mInformation.add(new Pair<String,String>(version, SpeechSynthesis.getVersion()));
  175. final String statusText;
  176. switch (mState) {
  177. case ERROR:
  178. statusText = getString(R.string.error_message);
  179. break;
  180. case DOWNLOAD_FAILED:
  181. statusText = getString(R.string.voice_data_failed_message);
  182. break;
  183. default:
  184. if (!getPackageName().equals(mTts.getDefaultEngine())) {
  185. statusText = getString(R.string.set_default_message);
  186. } else {
  187. statusText = null;
  188. }
  189. break;
  190. }
  191. if (statusText != null) {
  192. final String statusLabel = getString(R.string.status);
  193. mInformation.add(new Pair<String,String>(statusLabel, statusText));
  194. }
  195. mInformationView.notifyDataSetChanged();
  196. }
  197. /**
  198. * Handles the result of voice data verification. If verification fails
  199. * following a successful installation, displays an error dialog. Otherwise,
  200. * either launches the installer or attempts to initialize the TTS engine.
  201. *
  202. * @param resultCode The result of voice data verification.
  203. * @param data The intent containing available voices.
  204. */
  205. private void onDataChecked(int resultCode, Intent data) {
  206. if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  207. if (mDownloadedVoiceData) {
  208. Log.e(TAG, "Voice data check failed (error code: " + resultCode + ").");
  209. setState(State.ERROR);
  210. populateInformationView();
  211. } else {
  212. downloadVoiceData();
  213. }
  214. return;
  215. }
  216. mVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
  217. initializeEngine();
  218. populateInformationView();
  219. }
  220. /**
  221. * Handles the result of voice data installation. Either shows a failure
  222. * dialog or launches the voice data verifier.
  223. *
  224. * @param resultCode
  225. */
  226. private void onDataDownloaded(int resultCode) {
  227. if (resultCode != RESULT_OK) {
  228. Log.e(TAG, "Voice data download failed.");
  229. setState(State.DOWNLOAD_FAILED);
  230. return;
  231. }
  232. mDownloadedVoiceData = true;
  233. checkVoiceData();
  234. }
  235. /**
  236. * Handles the result of TTS engine initialization. Either displays an error
  237. * dialog or populates the activity's UI.
  238. *
  239. * @param status The TTS engine initialization status.
  240. */
  241. private void onInitialized(int status) {
  242. if (status != TextToSpeech.SUCCESS) {
  243. Log.e(TAG, "Initialization failed (status: " + status + ").");
  244. setState(State.ERROR);
  245. } else {
  246. setState(State.SUCCESS);
  247. }
  248. populateInformationView();
  249. }
  250. @Override
  251. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  252. switch (requestCode) {
  253. case REQUEST_CHECK:
  254. onDataChecked(resultCode, data);
  255. break;
  256. case REQUEST_DOWNLOAD:
  257. onDataDownloaded(resultCode);
  258. break;
  259. case REQUEST_DEFAULT:
  260. initializeEngine();
  261. break;
  262. }
  263. super.onActivityResult(requestCode, resultCode, data);
  264. }
  265. private final TextToSpeech.OnInitListener mInitListener = new TextToSpeech.OnInitListener() {
  266. @Override
  267. public void onInit(int status) {
  268. mHandler.obtainMessage(TTS_INITIALIZED, status, 0).sendToTarget();
  269. }
  270. };
  271. private static class EspeakHandler extends Handler {
  272. private WeakReference<eSpeakActivity> mActivity;
  273. public EspeakHandler(eSpeakActivity activity)
  274. {
  275. mActivity = new WeakReference<eSpeakActivity>(activity);
  276. }
  277. @Override
  278. public void handleMessage(Message msg) {
  279. switch (msg.what) {
  280. case TTS_INITIALIZED:
  281. mActivity.get().onInitialized(msg.arg1);
  282. break;
  283. }
  284. }
  285. }
  286. private final Handler mHandler = new EspeakHandler(this);
  287. private void launchGeneralTtsSettings()
  288. {
  289. Intent intent;
  290. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  291. {
  292. // The Text-to-Speech settings is a Fragment on 3.x:
  293. intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
  294. intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, "com.android.settings.TextToSpeechSettings");
  295. intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, intent.getExtras());
  296. }
  297. else
  298. {
  299. // The Text-to-Speech settings is an Activity on 2.x and 4.x:
  300. intent = new Intent(ACTION_TTS_SETTINGS);
  301. }
  302. startActivityForResult(intent, REQUEST_DEFAULT);
  303. }
  304. }