Android使用文本转语音播报

添加权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

一:界面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical"
        android:gravity="center">
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TTS离线语音合成语音播报使用"
            android:hint="请输入要转换为语音的文本"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="语音转换"/>

    </LinearLayout>

</RelativeLayout>

二:调用

package com.example.texttospeech;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;

public class MainActivity extends AppCompatActivity{
    private EditText editText;
    private Button button;
    private TextToSpeech toSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTextToSpeech();

        initView();
    }

    private void initView() {
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                    play();
                } else {
                    Toast.makeText(MainActivity.this, "要转化为语音的文本不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 初始化语音
    private void initTextToSpeech() {
        toSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    // 设置首选项为中文
                    int result =toSpeech.setLanguage(Locale.US);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(MainActivity.this, "语言数据丢失或不支持中文", Toast.LENGTH_SHORT).show();
                    }
                    play();
                }
            }
        });
        // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
        toSpeech.setPitch(1.5f);
        // 设置语速
        toSpeech.setSpeechRate(1.0f);
    }

    // 播放语音
    private void play(){
        if (toSpeech != null && !toSpeech.isSpeaking()) {
            toSpeech.speak(editText.getText().toString().trim(), TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 页面可见,自动播放语音
        play();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 页面不可见,不管是否正在播放语音都被打断
        toSpeech.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 页面销毁,停止语音,释放资源
        if(toSpeech != null) {
            toSpeech.stop();
            toSpeech.shutdown();
        }
    }
}


版权声明:本文为Qxnedy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。