TextTospeech(Context context, TextTospeech.OnInitListenerlistener)
speak(String text,int queueMode,HashMap<String,String>params)
synthesizeToFile(Stringtext,HashMap<String,String>params,String filename)
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.US);
// 如果不支持所设置的语言
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE)
{
Toast.makeText(Speech.this, "TTS暂时不支持这种语言的朗读。", 50000)
.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, "声音记录成功!", 50000).show();
}
});
}
@Override
public void onDestroy()
{
// 关闭TextToSpeech对象
if (tts != null)
{
tts.shutdown();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有