你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2012-07-28] 来源:Linux社区  作者:wwj_748 [字体:大 中 小]

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

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

创建项目:Speech

运行效果:

Activity文件:Speech.java

  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(this, new 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. }  

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android数据存储 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款