Android的数据存储和IO – 自动朗读(TTS)

Linux大全评论143 views阅读模式

Android的数据存储和IO - 自动朗读(TTS)

自动朗读又是Android提供的另一种另类的IO,蛮不错的哦,支持对指定文本内容进朗读,学习完这个内容我立马就让它朗读:wwj is a good man.作为一个自我满足。

创建项目:Speech

运行效果:

  1. package wwj.speech;  
  2.   
  3. import java.util.Locale;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.speech.tts.TextToSpeech;  
  8. import android.speech.tts.TextToSpeech.OnInitListener;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. public class Speech extends Activity {  
  16.       
  17.     TextToSpeech tts;  
  18.     EditText editText;  
  19.     Button speech;  
  20.     Button record;  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         //初始化TextToSpeech对象   
  26.         tts = new TextToSpeech(thisnew OnInitListener() {  
  27.               
  28.             public void onInit(int status) {  
  29.                 // TODO Auto-generated method stub   
  30.                 //如果装载TTS引擎成功   
  31.                 if(status == TextToSpeech.SUCCESS){  
  32.                     //设置使用美式英语朗读   
  33.                     int result = tts.setLanguage(Locale.US);  
  34.                     //如果不支持所设置的语言   
  35.                     if(result != TextToSpeech.LANG_COUNTRY_AVAILABLE   
  36.                             && result != TextToSpeech.LANG_AVAILABLE){  
  37.                         Toast.makeText(Speech.this"TTS暂时不支持这种语言的朗读。"50000).show();  
  38.                     }  
  39.                 }  
  40.             }  
  41.         });  
  42.         editText = (EditText)findViewById(R.id.txt);  
  43.         speech = (Button) findViewById(R.id.speech);  
  44.         record = (Button) findViewById(R.id.record);  
  45.         speech.setOnClickListener(new OnClickListener() {  
  46.               
  47.             public void onClick(View v) {  
  48.                 // TODO Auto-generated method stub   
  49.                 //执行朗读   
  50.                 tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null);  
  51.             }  
  52.         });  
  53.         record.setOnClickListener(new OnClickListener() {  
  54.               
  55.             public void onClick(View v) {  
  56.                 // TODO Auto-generated method stub   
  57.                 //将朗读文本的音频记录到指定文件   
  58.                 tts.synthesizeToFile(editText.getText().toString(), null"/mnt/sdcard/sound.wav");  
  59.                 Toast.makeText(Speech.this"声音记录成功! "50000).show();  
  60.             }  
  61.         });  
  62.     }  
  63.     @Override  
  64.     protected void onDestroy() {  
  65.         // TODO Auto-generated method stub   
  66.         //关闭TextToSpeech对象   
  67.         if(tts != null){  
  68.             tts.shutdown();  
  69.         }  
  70.         super.onDestroy();  
  71.     }  
  72. }  

企鹅博客
  • 本文由 发表于 2020年9月6日 00:48:33
  • 转载请务必保留本文链接:https://www.qieseo.com/175942.html

发表评论