It is common to make TextView and EditText sound call applications. It is too troublesome to use the TTS voice engine! Therefore, you can use MediaPlayer to play audio files.
The following is the key code:
Key variable definition
Private TextView numInput; private int length = 0; // The current String length private int beforeLength = 0; // The last string length private MediaPlayer mediaPlayer; private ArrayList
MListeners = null; EditTextWatchTEL watch;
Initialization Method
watch = new EditTextWatchTEL();numInput.addTextChangedListener(watch);if (mListeners == null) {mListeners = new ArrayList
();}mListeners.add(watch);
Main Code
Private class EditTextWatchTEL implements TextWatcher {@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void afterTextChanged (Editable s) {if (length> 0) {if (beforeLength <= length) {beforeLength = length; List
List = new ArrayList
(); For (int I = 0; I <10; I ++) {list. add (String. valueOf (I);} String num = numInput. getText (). toString (). substring (beforeLength-1); if (list. contains (num) {int value = Integer. parseInt (num); switch (value) {case 0: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} // R. raw. zero is the audio file in the raw folder. The following figure shows mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. zero); mediaPlayer. start (); break; case 1: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. one); mediaPlayer. start (); break; case 2: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. two); mediaPlayer. start (); break; case 3: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. three); mediaPlayer. start (); break; case 4: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. four); mediaPlayer. start (); break; case 5: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. five); mediaPlayer. start (); break; case 6: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. six); mediaPlayer. start (); break; case 7: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. seven); mediaPlayer. start (); break; case 8: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. eight); mediaPlayer. start (); break; case 9: if (mediaPlayer! = Null) {// You must clear the player resources mediaPlayer. stop (); mediaPlayer. release (); mediaPlayer = null;} mediaPlayer = MediaPlayer. create (getApplicationContext (), R. raw. nine); mediaPlayer. start (); break; default: break ;}} else {beforeLength = length ;}}@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {length = numInput. getText (). length () ;}// remove the listener public void removeTextChangedListene R (TextWatcher watcher) {if (mListeners! = Null) {int I = mListeners. indexOf (watcher); if (I> = 0) {mListeners. remove (I) ;}} numInput. removeTextChangedListener (watcher) ;}@ Overrideprotected void onDestroy () {removeTextChangedListener (watch); super. onDestroy ();}
Record and share, let you grow together! Welcome to my other blog; my blog address: http://blog.csdn.net/caicongyang