android自學之TextToSpeech實現文字向語音的轉換

 

  1. package com.example.texttospeechexcercise; 
  2.  
  3. import java.util.Locale; 
  4. import java.util.Random; 
  5.  
  6. import android.os.Bundle; 
  7. import android.app.Activity; 
  8. import android.view.Menu; 
  9. import android.view.View; 
  10. import android.view.View.OnClickListener; 
  11. import android.widget.Button; 
  12. import android.widget.Toast; 
  13. import android.speech.tts.TextToSpeech; 
  14. import android.text.StaticLayout; 
  15. public class MainActivity extends Activity implements TextToSpeech.OnInitListener{ 
  16.       
  17.     String[] words=new String[]{"hello","dog","cat","bye"}; 
  18.     //static final Random RANDOM=new Random(); 
  19.     private TextToSpeech tSpeech; 
  20.     private Button button; 
  21.      
  22.     @Override 
  23.     protected void onCreate(Bundle savedInstanceState) { 
  24.         super.onCreate(savedInstanceState); 
  25.         setContentView(R.layout.activity_main); 
  26.         button=(Button) findViewById(R.id.button1); 
  27.         tSpeech=new TextToSpeech(getApplicationContext(), this); 
  28.         button.setOnClickListener(new OnClickListener() { 
  29.              
  30.             @Override 
  31.             public void onClick(View v) { 
  32.                 // TODO Auto-generated method stub 
  33.                 sayHello(); 
  34.             } 
  35.         }); 
  36.          
  37.     } 
  38.  
  39.     private void sayHello() { 
  40.         // TODO Auto-generated method stub 
  41.         int size=words.length; 
  42.         //返回一個僞隨機數,它是取自此隨機數生成器序列的、在 0(包括)和指定值(不包括)之間均勻分佈的 int 值。 
  43.         //nextInt 的常規協定是,僞隨機地生成並返回指定範圍中的一個 int 值。所有可能的 n 個 int 值的生成概率(大致)相同。 
  44.         int i=new Random().nextInt(size); 
  45.         String word=words[i]; 
  46.         tSpeech.speak(word,  TextToSpeech.QUEUE_FLUSH, null); 
  47.     } 
  48.     @Override 
  49.     protected void onDestroy() { 
  50.         // TODO Auto-generated method stub 
  51.         if (tSpeech!=null) { 
  52.              
  53.             tSpeech.stop(); 
  54.             tSpeech.shutdown(); 
  55.         } 
  56.          
  57.         super.onDestroy(); 
  58.     } 
  59.  
  60.     @Override 
  61.     public void onInit(int status) { 
  62.         // TODO Auto-generated method stub 
  63.         if (status==TextToSpeech.SUCCESS) { 
  64.         int result=tSpeech.setLanguage(Locale.US);   
  65.         if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED) { 
  66.             Toast.makeText(getApplicationContext(), "Language is not available.",1000).show(); 
  67.         } 
  68.         else { 
  69.             button.setEnabled(true); 
  70.             sayHello(); 
  71.         } 
  72.         }else { 
  73.             Toast.makeText(getApplicationContext(), "init failed"1000).show(); 
  74.         } 
  75.         } 
  76.     } 
  77.  
  78.      

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章