• 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. }

  • 相关阅读:
    通过空间占用和执行计划了解SQL Server的行存储索引
    软件测试/测试开发丨明确的编码规范,避免冗余和混乱
    BP神经网络
    某基金投资公司绩效考核设计项目成功案例纪实
    rac环境rman备份
    postswigger 靶场(CSRF)攻略-- 1.没有防御措施的 CSRF 漏洞
    C语言 | Leetcode C语言题解之第329题矩阵中的最长递增路径
    PlayWright(二十二)- allure插件(一)
    MMDeploy理解
    【Flink】需求实现之独立访客数量的计算 和 布隆过滤器的原理及使用
  • 原文地址:https://blog.csdn.net/Qxnedy/article/details/125484303