个性化阅读
专注于IT技术分析

android texttospeech教程

点击下载

本文概述

在android中,你可以借助TextToSpeech类将文本转换为语音。转换完成后,你可以播放或创建声音文件。

TextToSpeech类的构造方法

  • TextToSpeech(上下文上下文,TextToSpeech.OnInitListener)

TextToSpeech类的方法

TextToSpeech类的常用方法如下:

方法描述
int speak (String text, int queueMode, HashMap 将文本转换为语音。队列模式可以是QUEUE_ADD或QUEUE_FLUSH。请求参数可以为null, KEY_PARAM_STREAM, KEY_PARAM_VALUME等。
int setSpeechRate(float speed)它设置了语音的速度。
int setPitch(float speed)它设置演讲的音调。
int setLanguage (Locale loc)它设置语音的语言环境特定语言。
void shutdown()它释放由TextToSpeech Engine设置的资源。
int stop()它会中断当前说话(无论是播放还是渲染到文件), 并丢弃队列中的其他说话。

TextToSpeech.OnInitListener接口

你需要实现TextToSpeech.OnInitListener接口,以便在TextToSpeech引擎上执行事件处理。

TextToSpeech.OnInitListener接口的方法

此界面中只有一种方法。

方法描述
void onInit (int status)调用以指示TextToSpeech引擎初始化完成。状态可以是SUCCESS或ERROR。

Android TextToSpeech示例

让我们编写代码将文本转换为语音。

activity_main.xml

拖动一个textview,一个edittext和一个用于布局的按钮。现在,activity_main.xml文件将如下所示:

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="77dp"
        android:layout_marginTop="42dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="59dp"
        android:layout_marginTop="39dp"
        android:text="Speak" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="Enter Text:" />

</RelativeLayout>

活动类

让我们看看说出给定文本的代码。

package com.example.texttospeech;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button buttonSpeak;
private EditText editText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);

buttonSpeak.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        speakOut();
    }

});
}

@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
    tts.stop();
    tts.shutdown();
}
super.onDestroy();
}

@Override
public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {

    int result = tts.setLanguage(Locale.US);

    if (result == TextToSpeech.LANG_MISSING_DATA
            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e("TTS", "This Language is not supported");
    } else {
    	buttonSpeak.setEnabled(true);
        speakOut();
    }

} else {
    Log.e("TTS", "Initilization Failed!");
}

}

private void speakOut() {
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


你需要在Real Device(例如移动设备)上运行它以测试应用程序。


Next:带有速度和音高选项的TextToSpeech示例

赞(0)
未经允许不得转载:srcmini » android texttospeech教程

评论 抢沙发

评论前必须登录!