`

TextToSpeech 文本自动朗读

阅读更多
Android提供了自动朗读支持。

  • TextToSpeech(Context context, TextToSpeech.OnInitListener listener)
  • setLanguage(Locale loc)。如果调用setLanguage(Locale loc)的返回值是 TextToSpeech.LANG_COUNTRY_AVAILABLE 则说明当前TTS系统可以支持所设置的语言、国家选项。
  • speak(String text, int queueMode, HashMap<String,String> params)
  • synthesizeToFile(String text, HashMap<String,String> params, String filename)
  • TextToSpeech.QUEUE_FLUSH
  • TextToSpeech.QUEUE_ADD


归纳起来,使用TextToSpeech引擎的步骤如下:
(1)创建TextToSpeech对象,创建时传入OnInitListener监听器监听创建是否成功。
(2)设置TextToSpeech所使用语言、国家选项,通过返回值判断TTS是否支持该语言、国家选项。
(3)调用speak或synthesizeToFile方法。
(4)关闭TTS,释放资源。
src\org\crazyit\io\Speech.java
package org.crazyit.io;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (c), 2001-2014, F.L
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author F.L@126.com
 * @version 1.0
*/
public class Speech extends Activity
{
	TextToSpeech tts;
	EditText editText;
	Button speech;
	Button record;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 初始化TextToSpeech对象
		tts = new TextToSpeech(this, new OnInitListener()
		{
			@Override
			public void onInit(int status)
			{
				// 如果装载TTS成功
				if ( status == TextToSpeech.SUCCESS)
				{
					// 设置使用英式英语朗读
					int result = tts.setLanguage(Locale.UK);
					// 如果不支持所设置的语言
					if ( result != TextToSpeech.LANG_COUNTRY_AVAILABLE
						&& result != TextToSpeech.LANG_AVAILABLE)
					{
						Toast.makeText(Speech.this
							, "TTS暂时不支持这种语言的朗读。", Toast.LENGTH_LONG)
							.show();
						}
					}
				}
								
		});
		editText = (EditText) findViewById(R.id.txt);
		speech = (Button) findViewById(R.id.speech);
		record = (Button) findViewById(R.id.record);
		speech.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				// 执行朗读
				tts.speak(editText.getText().toString(),
					TextToSpeech.QUEUE_ADD, null);
			}
		});
		record.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				// 将朗读文本的音频记录到指定文件
				tts.synthesizeToFile(editText.getText().toString()
					, null, "/mnt/sdcard/sound.wav");
				Toast.makeText(Speech.this, "声音记录成功!"
						, Toast.LENGTH_LONG).show();
				}
			});
	}
										
	@Override
	public void onDestroy()
	{
		// 关闭TextToSpeec对象
		if ( tts != null)
		{
			tts.shutdown();
		}
	}		
}


res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center_horizontal"
	>
<EditText 
	android:id="@+id/txt"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:lines="5"
	/>
<LinearLayout
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:gravity="center_horizontal"
	>	
<Button 
	android:id="@+id/speech"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:text="@string/speech"
	/>
<Button 
	android:id="@+id/record"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:text="@string/record"
	/>	
</LinearLayout>	
</LinearLayout>


res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="hello">Hello World, Speek!</string>
	<string name="app_name">自动朗读</string>
	<string name="speech">朗读</string>
	<string name="record">记录声音</string>
</resources>



AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.crazyit.io"
	android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk
		android:minSdkVersion="10"
		android:targetSdkVersion="17" />
	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
	<application
		android:icon="@drawable/ic_launcher"
		android:label="@string/app_name">
		<activity
			android:name=".Speech"
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
</manifest>
分享到:
评论

相关推荐

    文本朗读(TextToSpeech)

    文本朗读技术,通常被称为TTS(Text To Speech),是一种将文本转换成语音输出的技术,广泛应用于各种领域,包括辅助视觉障碍者、提高电子阅读体验、智能助手、语音导航等。在Windows操作系统中,内置了TTS引擎,...

    Text To Speech的Android编程方法(实例源码下载)

    如果希望新文本追加到队列后面,可以使用`TextToSpeech.QUEUE_ADD`。 在应用不再需要TTS服务时,应释放资源,避免内存泄漏: ```java if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown...

    TextToSpeech_20210423.rar

    《TextToSpeech_20210423.rar》是一个关于文本转语音(Text-to-Speech, TTS)技术的资源包,其中包含了源代码和可能的剩余时间评估功能。这个工具允许用户将文本转化为可听的语音,广泛应用于各种应用场景,如辅助...

    C#文本语音朗读

    public class TextToSpeech { public void Speak(string text) { if (string.IsNullOrEmpty(text)) return; var synthesizer = new SpeechSynthesizer(); // 设置语音引擎,如需XP兼容,可能需要下载并设置...

    Android应用源码之调用安卓自带文本朗读.zip

    在Android应用开发中,有时我们需要实现一个功能,让应用程序能够自动朗读屏幕上的文本,这在教育、无障碍或辅助功能方面特别有用。本教程将详细讲解如何利用Android系统的内置文本朗读功能,即Text-to-Speech(TTS...

    Simple Text to Speech App using JavaScript

    在这个"Simple Text to Speech App using JavaScript"项目中,我们将深入探讨如何利用JavaScript实现文本转语音功能,这在游戏开发、无障碍设计、教育应用等领域都有广泛应用。 首先,JavaScript的Text-to-Speech...

    android 自动朗读功能

    在Android平台上,自动朗读...总之,Android的自动朗读功能是通过TextToSpeech类实现的,开发者可以通过配置和调用相关API来实现文本到语音的转换。通过熟练掌握这些知识点,可以为用户提供更便捷、个性化的交互体验。

    Text_to_Speech

    "Text_to_Speech",即文本转语音(TTS)技术,是一种将书面文本转换成可听见的语音输出的技术。这种技术广泛应用于各种领域,包括辅助视力障碍者、提高阅读效率、创建有声读物、智能语音助手以及自动化的语音导航...

    语音朗读 支持英文

    除了基本的文本朗读,Android的TextToSpeech还支持其他高级功能,如设置语速、音调和音量,以及播放音频文件等。例如,通过`setSpeechRate()`调整语速,`setPitch()`调整音调: ```java tts.setSpeechRate(1.2f); /...

    安卓Android源码——调用安卓自带文本朗读.zip

    TextToSpeech tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { // 初始化成功,可以调用speak()...

    Android开发之文本内容自动朗读功能实现方法

    这种自动朗读支持的英文名称为TextToSpeech,简称TTS。 借助于TTS的支持,可以在应用程序中动态地增加音频输出,从而改善用户体验。 Android的自动朗读支持主要通过TextTospeech来完成,该累提供了如下一个构造器: ...

    vb.net对文本内容信息进行朗读

    通过VB.NET的Text-to-Speech功能,我们可以轻松实现文本内容的朗读、速度控制、暂停以及音频文件的保存,从而增强应用程序的交互性和可用性。不过,这些功能的实现依赖于系统是否安装了相应的语音库,因此在实际应用...

    c# speech 中的 TTS 源码!!

    在C#中,Text To Speech (TTS) 技术是一种将文本转换为语音输出的功能,使得计算机能够“朗读”出屏幕上的文字。这对于视觉障碍的用户、学习语言的应用或者自动化过程中的语音反馈非常有用。C#提供了System.Speech库...

    易语言源码易语言文本朗读源码.rar

    在文本朗读方面,易语言可能使用了Windows操作系统提供的Text To Speech (TTS) 技术,也称为语音合成。Windows API中有许多接口可用于实现这一功能,如SAPI(Speech Application Programming Interface)。 首先,...

    利用WebAPI speechSynthesis,朗读英语单词

    `speechSynthesis` API 是浏览器内置的文本转语音(TTS, Text-to-Speech)功能,它可以将字符串转换为合成的语音输出。这个特性可以帮助用户,特别是视力障碍者或那些在阅读时需要额外帮助的人,更好地理解网页内容...

    PyPI 官网下载 | text2speech-0.1.7.tar.gz

    `text2speech`库是Python中的一个文本转语音(TTS,Text-to-Speech)工具,它允许开发者将文本转换成自然流畅的语音输出。这个库在教育、辅助技术、自动化脚本等多个领域都有广泛的应用。0.1.7是该库的一个版本号,...

    文本朗读器的设计与制作

    文本朗读器是一种将文本转换成语音输出的软件工具,它是语音合成技术(TTS, Text-to-Speech)的重要应用。这种技术使得计算机能够读出文本内容,为视觉障碍人士提供便利,同时也广泛应用于电话自动应答系统(IVR)、电子...

    口语自动朗读练习Excel表

    自动朗读功能的实现可能依赖于Microsoft Office中的Text-to-Speech(TTS)技术,这是一种将文字转化为可听见语音的工具,它可以模拟人类的声音,尽管可能没有真人那么自然,但对于学习新语言的音素和语调仍非常有用...

    文本朗读软件可将文本转换为声音文件

    SAPI是微软提供的一个强大的语音技术接口,它支持多种语音合成(TTS,Text-to-Speech)和语音识别(ASR,Automatic Speech Recognition)功能。C#开发者可以利用.NET框架中的System.Speech库,这是对SAPI的一个封装...

Global site tag (gtag.js) - Google Analytics