Android对TTS技术的支持

Android 1.6开始支持TTS(Text To Speech)技术,通过该技术可以将文本转换成语音。

TTS技术的核心是android.speech.tts.TextToSpeech类。要想使用TTS技术朗读文本,需要做两个工作:初始化TTS和指定要朗读的文本。在第1项工作中主要指定TTS朗读的文本的语言,第2项工作主要使用speak方法指定要朗读的文本。

在Android中使用TTS技术

TextToSpeech.OnInitListener.onInit用于初始化TTS

TextToSpeech.speak用于将文本转换为声音

Demo
 1 import java.util.Locale; 2  3 import android.annotation.SuppressLint; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.speech.tts.TextToSpeech; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.TextView;11 import android.widget.Toast;12 13 @SuppressLint("NewApi")14 15 /**16  * 朗读文本17  * 18  * 安卓本身的库,只支持英文。除非从网上更新下载其他语言包。19  * @author dr20  */21 public class Main extends Activity implements TextToSpeech.OnInitListener,22         OnClickListener {23     private TextToSpeech tts;24     private TextView textView;25 26     @SuppressLint("NewApi")27     @Override28     public void onCreate(Bundle savedInstanceState) {29         super.onCreate(savedInstanceState);30         setContentView(R.layout.main);31          32         tts = new TextToSpeech(this, this);33 34         Button button = (Button) findViewById(R.id.button);35         textView = (TextView) findViewById(R.id.textview);36         button.setOnClickListener(this);37     }38 39     public void onClick(View view) {40         tts.speak(textView.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);41     }42 43     @Override44     public void onInit(int status) {45         if (status == TextToSpeech.SUCCESS) {46             int result = tts.setLanguage(Locale.US);47             if (result == TextToSpeech.LANG_MISSING_DATA48                     || result == TextToSpeech.LANG_NOT_SUPPORTED) {49                 Toast.makeText(this, "Language is not available.",50                         Toast.LENGTH_LONG).show();51             }52         }53 54     }55 56 }

更多相关文章

  1. Android(安卓)TextInputLayout,打造 Material Design 风格的文本
  2. Android学习笔记(十三)text 文本输入
  3. android中利用Paint绘制文本使其居中
  4. Android控件复习之TextView与EditText
  5. Android(安卓)EditText常用属性
  6. Android控件笔记——使用TextView实现跑马灯效果
  7. Android---Android让文本输入框默认不获取焦点
  8. 笔记:TextView属性大全
  9. 自定义带提示的文本输入框

随机推荐

  1. Android中文API(137) —— LocalSocket
  2. Android之多线程断点下载
  3. Android(安卓)内核编绎错误解决方案
  4. Android(安卓)Looper中的MessageQueue消
  5. android 自动打包
  6. View requires API level 14 (current mi
  7. 【Android】Sensor框架HAL层解读
  8. WindowManagerService添加View流程
  9. android的socket开发注意事项
  10. android学习笔记二