android学习笔记53——自动朗读TTS

自动朗读TTS

android提供了自动朗读功能——其指的是支持可以对指定文本内容进行朗读,从而发出声音;

同时android的自动朗读支持还允许把文本对应的音频录制成音频文件,方便后续播放。

这种自动朗读支持的英文名称为:TextToSpeech,检测TTS.

TTS,可以在应用程序中动态地增加音频输出,从而提高用户体验。

Android的自动朗读支持通过TextToSpeech完成,该类提供了如下一个构造函数:

==>TextToSpeeech(Content content,TextToSpeeck.OnInitListener listener),当创建TextToSpeech对象时,必须先提供一个OnInitListener监听器——该监听器负责监听TextToSpeech的初始化效果。

程序获取TextToSpeeech对象后,可调用TextToSpeeech的setLanguage(Locale loc)方法设置该TTS发声引擎使用的语音、国家选项。

注意:

如果调用setLanguage(Locale loc)的返回值是:TextToSpeech.LANG_COUNTRY_AVALABLE,说明当前TTS系统可以支持所设置的语音、国家选项。

对TextToSpeech设置完成后,即可调用它的方法来朗读文本了,具体方法可参考TextToSpeech的API文档。

TextToSpeech类中最常用的两个方法如下:

1.speak(String text,int queueMode,HashMap<String,String> params);

2.synthesizeToFile(String text,HashMap<String,String> params,String fileName);

以上两个方法都是用于把text文字内容转换为音频,区别只是speak方法是播放转换的音频,而synthesizeToFile是把转换得到的音频保存成声音文件。

以上两个方法中的params都用于指定声音转换时的参数,speak()中的queueMode参数指定TTS的发音队列模式,该模式支持如下两个常量:

1.TextToSpeech.QUEUE_PLUSH:如果指定该模式,当TTS调用speak()时,它会中断当前实例正在运行的任务(也可理解为清除当前语音任务,转而执行新的语音任务)

2.TextToSpeech.QUEUE_ADD:如果指定该模式,当TTS调用speak()时,会把新的发音任务添加到当前发音任务列队之后——也就是等任务队列中的发音任务执行完成后再来执行speak()指定的发音任务。

注意:

当程序用完了TextToSpeech对象后,可以在Activity的OnDestory()中调用它的shutdown()来关闭TextToSpeech、释放其所占用资源。

总结:TextToSpeech使用步骤如下:

1.创建TextToSpeech对象,创建时传入OnInitListener监听器监听创建是否成功;

2.设置TextToSpeech所使用语言、国家选项,通过返回值判断TTS是否支持该语言、国家选项;

3.调用speak()或synthesizeToFile();

4.关闭TTS,回收资源。

实例如下:

布局文件==》
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is a dog" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnRecord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记录声音" />

        <Button
            android:id="@+id/btnRead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="朗读" />
    </LinearLayout>

</LinearLayout>

代码实现==>
package com.example.mytts;

import java.util.Locale;

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

public class MainActivity extends Activity
{
	EditText edit;
	TextToSpeech tts;

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

		edit = (EditText) this.findViewById(R.id.edit);
		Button btnRecord = (Button) this.findViewById(R.id.btnRecord);
		Button btnRead = (Button) this.findViewById(R.id.btnRead);

		btnRecord.setOnClickListener(new MyButtonOnClick());
		btnRead.setOnClickListener(new MyButtonOnClick());

		tts = new TextToSpeech(this, new OnInitListener()
		{
			@Override
			public void onInit(int status)
			{
				// 如果装载TTS引擎成功
				if (status == TextToSpeech.SUCCESS)
				{
					// 设置使用美式英语朗读
					int result = tts.setLanguage(Locale.US);
					// 如果不支持所设置的语言、国家
					if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
							&& result != TextToSpeech.LANG_AVAILABLE)
					{
						Toast.makeText(MainActivity.this, "TTS不支持本次设置语言、国家的朗读", 5000).show();
					}
				}
			}
		});
	}

	private class MyButtonOnClick implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btnRecord:
				// 将朗读文本的音频记录到指定文件
				tts.synthesizeToFile(edit.getText().toString(), null, "/mnt/sdcard/sound.wav");
				Toast.makeText(MainActivity.this, "声音记录成功", 5000).show();
				break;
			case R.id.btnRead:
				tts.speak(edit.getText().toString(), TextToSpeech.QUEUE_ADD, null);
				break;
			}
		}

	}

	@Override
	protected void onDestroy()
	{
		// super.onDestroy();
		if (tts != null)
			tts.shutdown();
	}

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

}
原文地址:https://www.cnblogs.com/YYkun/p/5977316.html