android 自动朗读,Android TextToSpeech多国语言自动朗读

实现的效果如下:

彩色圆角按钮的实现可以参考我的这篇文章http://blog.csdn.net/ldld1717/article/details/52314344

工程的结构见下图:

AutoSpeech.java代码见下:

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();

}

}

}

layout中main.xml代码如下:

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">

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" />

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" />

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" />

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"/>

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"/>

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"/>

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"/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值