• Android使用文本转语音播报


    一:界面布局

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity"
    8. android:padding="20dp">
    9. <LinearLayout
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:layout_centerVertical="true"
    13. android:orientation="vertical"
    14. android:gravity="center">
    15. <EditText
    16. android:id="@+id/editText"
    17. android:layout_width="match_parent"
    18. android:layout_height="wrap_content"
    19. android:gravity="center"
    20. android:text="TTS离线语音合成语音播报使用"
    21. android:hint="请输入要转换为语音的文本"/>
    22. <Button
    23. android:id="@+id/button"
    24. android:layout_width="wrap_content"
    25. android:layout_height="wrap_content"
    26. android:layout_marginTop="50dp"
    27. android:text="语音转换"/>
    28. </LinearLayout>
    29. </RelativeLayout>

    二:调用

    1. package com.example.texttospeech;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.media.MediaPlayer;
    4. import android.os.Bundle;
    5. import android.speech.tts.TextToSpeech;
    6. import android.text.TextUtils;
    7. import android.view.View;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. import android.widget.Toast;
    11. import java.io.IOException;
    12. import java.util.HashMap;
    13. import java.util.Locale;
    14. public class MainActivity extends AppCompatActivity{
    15. private EditText editText;
    16. private Button button;
    17. private TextToSpeech toSpeech;
    18. @Override
    19. protected void onCreate(Bundle savedInstanceState) {
    20. super.onCreate(savedInstanceState);
    21. setContentView(R.layout.activity_main);
    22. initTextToSpeech();
    23. initView();
    24. }
    25. private void initView() {
    26. editText = findViewById(R.id.editText);
    27. button = findViewById(R.id.button);
    28. button.setOnClickListener(new View.OnClickListener() {
    29. @Override
    30. public void onClick(View v) {
    31. if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
    32. play();
    33. } else {
    34. Toast.makeText(MainActivity.this, "要转化为语音的文本不能为空", Toast.LENGTH_SHORT).show();
    35. }
    36. }
    37. });
    38. }
    39. // 初始化语音
    40. private void initTextToSpeech() {
    41. toSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    42. @Override
    43. public void onInit(int status) {
    44. if (status == TextToSpeech.SUCCESS) {
    45. // 设置首选项为中文
    46. int result =toSpeech.setLanguage(Locale.US);
    47. if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {
    48. Toast.makeText(MainActivity.this, "语言数据丢失或不支持中文", Toast.LENGTH_SHORT).show();
    49. }
    50. play();
    51. }
    52. }
    53. });
    54. // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
    55. toSpeech.setPitch(1.5f);
    56. // 设置语速
    57. toSpeech.setSpeechRate(1.0f);
    58. }
    59. // 播放语音
    60. private void play(){
    61. if (toSpeech != null && !toSpeech.isSpeaking()) {
    62. toSpeech.speak(editText.getText().toString().trim(), TextToSpeech.QUEUE_FLUSH, null);
    63. }
    64. }
    65. @Override
    66. protected void onStart() {
    67. super.onStart();
    68. // 页面可见,自动播放语音
    69. play();
    70. }
    71. @Override
    72. protected void onStop() {
    73. super.onStop();
    74. // 页面不可见,不管是否正在播放语音都被打断
    75. toSpeech.stop();
    76. }
    77. @Override
    78. protected void onDestroy() {
    79. super.onDestroy();
    80. // 页面销毁,停止语音,释放资源
    81. if(toSpeech != null) {
    82. toSpeech.stop();
    83. toSpeech.shutdown();
    84. }
    85. }
    86. }

  • 相关阅读:
    webserver项目
    Ribbon负载均衡的深度分析和使用
    服务器数据恢复—nas硬盘故障导致raid6失效、存储无法访问的数据恢复案例
    数据结构-二叉树的前、中、后序遍历
    MySQL:事务1(锁与隔离级别)
    软件测试面试题(持续更新中),欢迎大家一起完善
    C++学习之路-智能指针
    Apache APISIX遇到504超时的解决办法
    C#正则将字符替换为其它,正则排除中文
    nginx出现: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
  • 原文地址:https://blog.csdn.net/Qxnedy/article/details/125484303