把合成的语音以音频文件的形式保存在系统里,然后就可以直接用播放音频文件的方式来播放。
这个功能调用的函数为:
public int synthesizeToFile ( String text, HashMap < String , String > params, String filename)
第一个参数为要进行语音合成的文本;第二个参数我们在上一个Demo中有所介绍,是一个键值对形式的HashMap类型变量,可以设置语音片段的ID等;第三个参数为保存到系统中的文件名。
当你想和朋友分享一份精彩的文本合成语音后的效果时,你可以使用这个功能把它保存为音频文件发送给朋友,这样即使朋友的手机不具备TTS功能,也可以用播放音频的方式分享到;当你要对同一段较长的文本多次进行语音合成时,你可以把这段文本的语音保存为音频文件,然后使用时播放,这样会更省资源,运行速度更快,因为使用TTS是比较费资源的一个过程。因此我们会在某些场合用到这个功能。
下面我们就用这个功能完成一个Demo例子,当你害羞当面向她表白你对她的喜欢时,让Android帮你语音合成你想说的话,然后你就可以向她发送保存了对她喜欢的这个音频文件。
布局代码:
<?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"
>
<EditText android:id="@+id/inputText"
android:hint="Input the text here!"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button android:text="Speak"
android:id="@+id/speakBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:enabled="false"
></Button>
<TextView android:id="@+id/filenameLabel"
android:text="Save as:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></TextView>
<EditText android:id="@+id/filenameText"
android:hint="Input the saving file name here!"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
></EditText>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button android:id="@+id/recordBtn"
android:text="Record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
<Button android:id="@+id/playBtn"
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
></Button>
</LinearLayout>
</LinearLayout>
Java代码:
package com.example.androidttsdemo3;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.media.MediaPlayer ;
import java.io.File;
public class MainActivity extends Activity implements OnInitListener{
//实现初始接口
/** Called when the activity is first created. */
//定义变量
private EditText inputText = null;
private Button speakBtn = null;
private EditText filenameText = null;
private Button recordBtn = null;
private Button playBtn = null;
private TextToSpeech mTts;
private static final String TAG = "TTS Demo";
private static final String loveConfession = "Lan Hua, I love you. ";
private String loveFileName = null;
private File loveFile = null;
private MediaPlayer player = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//创建TextToSpeech实例,初始化完成后会调用OnInitListener(第二个参数)的回调函数
mTts = new TextToSpeech(this,
this // TextToSpeech.OnInitListener
);
//设置控件
inputText = (EditText)findViewById(R.id.inputText);
speakBtn = (Button)findViewById(R.id.speakBtn);
filenameText = (EditText)findViewById(R.id.filenameText);
recordBtn = (Button)findViewById(R.id.recordBtn);
playBtn = (Button)findViewById(R.id.playBtn);
inputText.setText(loveConfession);
filenameText.setText("/sdcard/love.wav");
speakBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//朗读输入框里的内容
mTts.speak(inputText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
}
});
recordBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//把TTS语音合成的结果保存为音频文件
loveFileName = filenameText.getText().toString();
loveFile = new File(loveFileName);
if(loveFile.exists())
{
loveFile.delete();
}
//把语音合成的结果保存到文件中
if(TextToSpeech.SUCCESS == mTts.synthesizeToFile(inputText.getText().toString(), null, loveFileName))
{
Toast.makeText(getBaseContext(), "sound file created!", Toast.LENGTH_SHORT).show();
playBtn.setEnabled(true);
}
else
{
Toast.makeText(getBaseContext(), "failed to create sound file!", Toast.LENGTH_SHORT).show();
}
}
});
playBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//播放保存着的音频文件
try
{
player = new MediaPlayer();
player.setDataSource(loveFileName);
player.prepare();
player.start();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(), "failed to play sound file!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
public void onInit(int status) {
// TODO Auto-generated method stub
//TTS Engine初始化完成
if(status == TextToSpeech.SUCCESS)
{
int result = mTts.setLanguage(Locale.US);
//设置发音语言
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
//判断语言是否可用
{
Log.v(TAG, "Language is not available");
speakBtn.setEnabled(false);
}
else
{
speakBtn.setEnabled(true);
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
//释放TTS的资源
if(mTts != null)
{
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
}