Android uses voice files to make TextView and EditText sound

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.