经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现自动朗读功能(TTS)
来源:jb51  时间:2021/8/26 17:21:00  对本文有异议

前言: Android提供了自动朗读支持。可以对指定文本内容进行朗读,从而发生声音;还允许把文本对应的音频录制成音频文件,方便以后播放。Android的自动朗读主要通过TextToSpeech来完成,构造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);当创建TextToSpeech对象时,必须先提供一个OnInitListener监听器——负责监听TextToSpeech的初始化结果。

效果图如下:

使用TextToSpeech的步骤如下:

1、创建TextToSpeech对象,创建时传入OnInitListener监听器监听示范创建成功。
2、设置TextToSpeech所使用语言国家选项,通过返回值判断TTS是否支持该语言、国家选项。
3、调用speak()或synthesizeToFile方法。
4、关闭TTS,回收资源。

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical">
  9.  
  10. <EditText
  11. android:id="@+id/input_text"
  12. android:layout_marginTop="20dp"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"/>
  15.  
  16. <LinearLayout
  17. android:layout_marginTop="10dp"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content">
  20.  
  21. <Button
  22. android:id="@+id/speech"
  23. android:text="Speech"
  24. android:layout_width="wrap_content"
  25. android:layout_weight="1"
  26. android:layout_height="wrap_content"/>
  27.  
  28. <Button
  29. android:id="@+id/record"
  30. android:text="Record"
  31. android:layout_weight="1"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"/>
  34.  
  35. </LinearLayout>
  36.  
  37. </LinearLayout>

Activity文件

  1. public class SpeechActivity extends AppCompatActivity {
  2.  
  3. private EditText input;
  4. private Button speech,record;
  5.  
  6. private TextToSpeech textToSpeech;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_speech);
  12.  
  13. textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
  14. @Override
  15. public void onInit(int status) {
  16. if (status == textToSpeech.SUCCESS) {
  17. int result = textToSpeech.setLanguage(Locale.CHINA);
  18. if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
  19. && result != TextToSpeech.LANG_AVAILABLE){
  20. Toast.makeText(SpeechActivity.this, "TTS暂时不支持这种语音的朗读!",
  21. Toast.LENGTH_SHORT).show();
  22. }
  23. }
  24. }
  25. });
  26.  
  27. input = (EditText) findViewById(R.id.input_text);
  28. speech = (Button) findViewById(R.id.speech);
  29. record = (Button) findViewById(R.id.record);
  30.  
  31. speech.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View view) {
  34. textToSpeech.speak(input.getText().toString(),
  35. TextToSpeech.QUEUE_ADD, null);
  36. }
  37. });
  38.  
  39. record.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View view) {
  42. String inputText = input.getText().toString();
  43. HashMap<String, String> myHashRender = new HashMap<>();
  44. myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);
  45. textToSpeech.synthesizeToFile(inputText, myHashRender,
  46. "/mnt/sdcard/my_recorder_audios/sound.wav");
  47. Toast.makeText(SpeechActivity.this, "声音记录成功。", Toast.LENGTH_SHORT).show();
  48. }
  49. });
  50. }
  51.  
  52. @Override
  53. protected void onDestroy() {
  54. if (textToSpeech != null)
  55. textToSpeech.shutdown();
  56. super.onDestroy();
  57. }
  58. }

这里我们使用的是中文,int result = textToSpeech.setLanguage(Locale.CHINA);你也可以根据自己的需求更改为其他支持的语言。

最后在AndroidManifest.xml中加入权限:

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

总结:通过使用Android提供的TTS,我们可以对指定文本内容进行朗读,从而发生声音;还允许把文本对应的音频录制成音频文件,保存到本地。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号