首页 > 其他分享 >Android实战--语音合成TTS

Android实战--语音合成TTS

时间:2022-11-11 13:01:55浏览次数:78  
标签:EditText01 TextToSpeech tts -- TTS import layout Android android


接着上一节讨论的问题,本DEMO中会用到TTS语音合成,我们下面介绍一个同样原理的小例子

看一下布局文件很简单:

 

<?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:background="@android:color/white"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input something to speak..."
>
</EditText>
<ImageButton
android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_btn_speak_now"
>
</ImageButton>
</LinearLayout>

下面是Activity:

package irdc.ex07_18;

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.ImageButton;
import android.widget.EditText;

public class EX07_18 extends Activity
{
public static String TAG = "EX07_18_DEBUG";
private TextToSpeech tts;
private EditText EditText01;
private ImageButton ImageButton01;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* 传入context及OnInitListener */
tts = new TextToSpeech(this, ttsInitListener);

EditText01 = (EditText) this.findViewById(R.id.EditText01);
ImageButton01 = (ImageButton) this.findViewById(R.id.ImageButton01);

ImageButton01.setOnClickListener(new ImageButton.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (EditText01.getText().length() > 0)
{
/* 传入要说的字符串 */
tts.speak(EditText01.getText().toString(), TextToSpeech.QUEUE_FLUSH,
null);
} else
{
/* 无输入字符串时 */
tts.speak("Nothing to say", TextToSpeech.QUEUE_FLUSH, null);
}

}

});
}

private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener()
{

@Override
public void onInit(int status)
{
// TODO Auto-generated method stub
/* 使用美国时区目前不支持中文 */
Locale loc = new Locale("us", "", "");
/* 检查是否支持输入的时区 */
if (tts.isLanguageAvailable(loc) == TextToSpeech.LANG_AVAILABLE)
{
/* 设定语言 */
tts.setLanguage(loc);
}
tts.setOnUtteranceCompletedListener(ttsUtteranceCompletedListener);
Log.i(TAG, "TextToSpeech.OnInitListener");
}

};
private TextToSpeech.OnUtteranceCompletedListener ttsUtteranceCompletedListener = new TextToSpeech.OnUtteranceCompletedListener()
{
@Override
public void onUtteranceCompleted(String utteranceId)
{
// TODO Auto-generated method stub
Log.i(TAG, "TextToSpeech.OnUtteranceCompletedListener");
}
};

@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
/* 释放TextToSpeech的资源 */
tts.shutdown();
Log.i(TAG, "tts.shutdown");
super.onDestroy();
}

}

运行实例效果:

Android实战--语音合成TTS_TTS

TTS了解了,具体可以用到哪些地方为你的应用添彩,就看你个人的创意了。

应用下载地址:

                                       ​​http://openbox.mobilem.360.cn/index/d/sid/2966005​

                                       ​​http://android.myapp.com/myapp/detail.htm?apkName=com.yayun.gitlearning​

欢迎下载,有问题多交流!(喜欢的请关注我,谢谢!)

 

 

标签:EditText01,TextToSpeech,tts,--,TTS,import,layout,Android,android
From: https://blog.51cto.com/u_15866446/5844738

相关文章

  • Android实战--小DEMO(JAVA关键字学习)一
    学习技术最好的方式就是实战,看书看不到的东西太多了,实际操作时会碰到各种书本里提不到的问题,解决这些问题会迅速提升你的能力,你是一个solider,最好成长的方式就是实战。下面......
  • 最短路板子
    floyedO(n^3) f[i][j]=min(f[i][j],f[i][k]+f[k][j]) memset(f,inf,sizeof(f));for(i=1;i<=m;i++)cin>>x>>y>>z,f[x][y]=f[y][x]=z;for......
  • P8817 假期计划 Sol
    看到数据范围,很容易想到平方。由于是双向边,所以很容易想到其实四个点可以被拆成两部分,两部分本质一样,可以一起处理。考虑枚举中转点\(x,y\),可以想到预处理与\(x\)距离......
  • Android错误之HAX is not installed on the machine(MAC OS)
    为了更好的工作和学习,最近新入手一台MacBookpro。在装Androidstudio时遇到如下问题:解决办法如下:点击:https://software.intel.com/en-us/android/articles/intel-hardware......
  • 一道zhangying机试题
    功能很好实现,但是用到的设计模式没有搞清楚,有一句话没太懂:每个调节项目调节时触发的动作会修改,不知道是什么含义,有明白的童鞋指导一下,选用何种设计模式。下面看一下功能代码......
  • 学生之家-6道练习题
    让用户输入一个数判断其是奇数还是偶数并把结果输出输入一个溶液的ph值试判断该溶液是酸性还是碱性或是中性溶液并把结果输出(常温25℃条件下)给定一个年份判断是否是......
  • Android错误之setAdapter(adapter)空指针nullPointer 解决办法
    在设置adapter时,会经常遇到如下错误:空指针错误如果setAdapter报空指针,那么可用如下语句调试,找出到底哪里产生空指针hisList=(ListView)findViewById(R.id.oillist);......
  • Android错误之--activity_main cannot be resolved or is not a field
    一般在copy别人的项目中会容易出现本错误,截图如下:出现本错误的一般有两种情况第一种情况:导包错误--检查import,找到这个:删除之,再重新导入含有包名的R文件。第二种情况:本情况......
  • PowerApps Default Environment: Important things
    Whatisthedefaultenvironment?Adefaultenvironmentisaparticulartypeofproductionenvironment.Eachtenanthasadefaultenvironmentthat’screatedau......
  • Python编码处理
    xxx.encoding=设置编码属性如:request中返回resp.encoding='utf-8'str.encode()-----转换编码----操作#---------------------编码处理--------------------......