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>


layout中main.xml代码如下:

<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
原创粉丝点击