自动朗读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,回收资源。
实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 布局文件==》 <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 ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本