Android TextToSpeech多国语言自动朗读
来源:互联网 发布:raft 算法 三副本 编辑:程序博客网 时间:2024/06/11 09:54
实现的效果如下:
彩色圆角按钮的实现可以参考我的这篇文章http://blog.csdn.net/ldld1717/article/details/52314344
工程的结构见下图:
AutoSpeech.java代码见下:
<span style="font-size:14px;">package com.example.leidong.autospeech;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.Locale;public class AutoSpeech extends Activity { TextToSpeech textToSpeech; EditText editText; //美式英语 Button english1; //英式英语 Button english2; //法语 Button french; //德语 Button germany; //意大利语 Button italian; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText); english1 = (Button) findViewById(R.id.english1); english2 = (Button) findViewById(R.id.english2); french = (Button) findViewById(R.id.french); germany = (Button) findViewById(R.id.germany); italian = (Button) findViewById(R.id.italian); /**********************************************/ textToSpeech = new TextToSpeech(AutoSpeech.this, new TextToSpeech.OnInitListener() { @Override public void onInit(int i) { //如果装载TTS引擎成功 if (i == TextToSpeech.SUCCESS) { /*美式英语按钮监听*/ english1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //设置使用美式英语朗读 int result = textToSpeech.setLanguage(Locale.US); //如果不支持所设置的语言 if ((result != textToSpeech.LANG_COUNTRY_AVAILABLE) && (result != TextToSpeech.LANG_AVAILABLE)) { Toast.makeText(AutoSpeech.this, "暂时不支持这种语言的朗读", Toast.LENGTH_SHORT) .show(); } //执行朗读 textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); /*英式英语按钮监听*/ english2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //设置使用英式英语朗读 int result = textToSpeech.setLanguage(Locale.UK); //如果不支持所设置的语言 if ((result != textToSpeech.LANG_COUNTRY_AVAILABLE) && (result != TextToSpeech.LANG_AVAILABLE)) { Toast.makeText(AutoSpeech.this, "暂时不支持这种语言的朗读", Toast.LENGTH_SHORT) .show(); } //执行朗读 textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); /*法语按钮监听*/ french.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //设置使用法语朗读 int result = textToSpeech.setLanguage(Locale.FRANCE); //如果不支持所设置的语言 if ((result != textToSpeech.LANG_COUNTRY_AVAILABLE) && (result != TextToSpeech.LANG_AVAILABLE)) { Toast.makeText(AutoSpeech.this, "暂时不支持这种语言的朗读", Toast.LENGTH_SHORT) .show(); } //执行朗读 textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); /*德语按钮监听*/ germany.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //设置使用德语朗读 int result = textToSpeech.setLanguage(Locale.GERMAN); //如果不支持所设置的语言 if ((result != textToSpeech.LANG_COUNTRY_AVAILABLE) && (result != TextToSpeech.LANG_AVAILABLE)) { Toast.makeText(AutoSpeech.this, "暂时不支持这种语言的朗读", Toast.LENGTH_SHORT) .show(); } //执行朗读 textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); /*意大利语按钮监听*/ italian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //设置使用意大利语朗读 int result = textToSpeech.setLanguage(Locale.ITALIAN); //如果不支持所设置的语言 if ((result != textToSpeech.LANG_COUNTRY_AVAILABLE) && (result != TextToSpeech.LANG_AVAILABLE)) { Toast.makeText(AutoSpeech.this, "暂时不支持这种语言的朗读", Toast.LENGTH_SHORT) .show(); } //执行朗读 textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); } } }); /**********************************************/ } public void onDestroy(){ //关闭TextToSpeech对象 if(textToSpeech != null){ textToSpeech.shutdown(); } }}</span>
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#2B2B2B"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5pt" android:layout_marginRight="5pt" android:layout_marginTop="10pt" android:text="请在下方输入需要朗读的句子:" android:textSize="10pt" android:textColor="#ff00cc" android:textStyle="bold" android:id="@+id/textView" android:layout_gravity="center_horizontal" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10pt" android:layout_marginRight="10pt" android:layout_marginTop="10pt" android:id="@+id/editText" android:textSize="15pt" android:textColor="#ffffff" android:textStyle="italic" android:singleLine="false" android:layout_gravity="center_horizontal" /> <!--美式英语朗读--> <Button android:id="@+id/english1" android:layout_width="274dp" android:layout_height="wrap_content" android:layout_marginLeft="25pt" android:layout_marginTop="5pt" android:text="美式英语朗读" android:textSize="10pt" android:background="@drawable/shape1" /> <!--英式英语朗读--> <Button android:id="@+id/english2" android:layout_width="274dp" android:layout_height="wrap_content" android:layout_marginLeft="25pt" android:layout_marginTop="10pt" android:text="英式英语朗读" android:textSize="10pt" android:background="@drawable/shape2"/> <!--法语朗读--> <Button android:id="@+id/french" android:layout_width="274dp" android:layout_height="wrap_content" android:layout_marginLeft="25pt" android:layout_marginTop="10pt" android:text="法语朗读" android:textSize="10pt" android:background="@drawable/shape1"/> <!--德语朗读--> <Button android:id="@+id/germany" android:layout_width="274dp" android:layout_height="wrap_content" android:layout_marginLeft="25pt" android:layout_marginTop="10pt" android:text="德语朗读" android:textSize="10pt" android:background="@drawable/shape2"/> <!--意大利语朗读--> <Button android:id="@+id/italian" android:layout_width="274dp" android:layout_height="wrap_content" android:layout_marginLeft="25pt" android:layout_marginTop="10pt" android:text="意大利语朗读" android:textSize="10pt" android:background="@drawable/shape1"/></LinearLayout></span>
0 0
- Android TextToSpeech多国语言自动朗读
- TextToSpeech自动朗读使用
- 使用TextToSpeech实现文本转音频(自动朗读)
- 使用TextToSpeech朗读文字
- Android 自动朗读TT
- android 自动朗读功能
- Android 多国语言
- Android 自动朗读(TTS)
- Android中的自动朗读(TTS)
- Android自动朗读(TTS)
- Android联合第三方实现使用TextToSpeech实现语音朗读的功能
- android多国语言使用
- Android 多国语言文件夹
- android多国语言文件夹
- android多国语言文件夹
- android多国语言使用
- android多国语言使用
- android 多国语言汇总
- HashMap源码解析
- mysql分组查询(group by)
- ITSA [C_MM058-中] 二項式求解
- 【腾讯TMQ】JAVA代码覆盖率工具JaCoCo-实践篇
- 用python实现简单的遗传算法
- Android TextToSpeech多国语言自动朗读
- 上传照片,调用系统照片
- 怎样把PDF转换成WORD简单有效
- ViewPager无线轮播
- XAMPP安装之后,还用装MYSQL吗?怎样以cmd操作MYSQL,怎样进入设置?
- redis-cli命令
- 怎么样在XAMPP上建立多个域名的站点
- 【重点】深入了解android平台的jni---注册native函数
- 从JKS文件中读取密钥