2018 Android 文字转语音(中文) TextToSpeech+科大讯飞语音引擎3.0

最近项目中需要用到文字转语音。
本来是想使用朗读女生成的声音文件放到项目资源中进行播放。
但是产品要求改成动态的。于是就用了Google为我们封装好的类TTS,即[TextToSpeech]:大家可以看下详细文档。

代码其实不多,但是写完之后测试就有问题,没声音,,,,看了之后才知道谷歌这个官方api不支持中文。。很Tmd.
给大家科普下:
文字转语音的引擎:

  • com.svox.pico 系统自带不支持中文语音
  • com.svox.classic 搜svox搜到的,和上面类似不支持中文
  • com.google.android.tts 谷歌文字转语音引擎,不支持5.0以下系统,大小18.85M
  • com.iflytek.speechcloud 科大讯飞语音引擎3.0,支持4.0以上系统,大小28.6M
  • com.baidu.duersdk.opensdk 度秘语音引擎3.0 不支持5.0以下系统,大小12.53M
  • com.iflytek.tts 科大讯飞语音合成,较老,不支持7.0以上系统,大小9.44M

引擎下载地址:
https://download.csdn.net/download/sqq_yj/10649462
-
需要再手机设置中修改:
语言和输入法 ==》 文字转语音(TTS)输出 ==》首选引擎由默认的Pico TTS 改为 科大讯飞语音引擎3.0

2018 Android 文字转语音(中文) TextToSpeech+科大讯飞语音引擎3.0_第1张图片

接下来就是代码:

package com.sgq.texttospeech;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener {
    private Button speechBtn; // 按钮控制开始朗读
    private EditText speechTxt; // 需要朗读的内容
    private TextToSpeech textToSpeech; // TTS对象

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        speechBtn = (Button) findViewById(R.id.btn_read);
        speechBtn.setOnClickListener(this);
        speechTxt = (EditText) findViewById(R.id.et_Text);
        textToSpeech = new TextToSpeech(this, this); // 参数Context,TextToSpeech.OnInitListener
    }

    @Override
    protected void onStop() {
        super.onStop();
        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
        textToSpeech.shutdown(); // 关闭,释放资源
    }

    /**
     * 用来初始化TextToSpeech引擎
     * status:SUCCESS或ERROR这2个值
     * setLanguage设置语言,帮助文档里面写了有22种
     * TextToSpeech.LANG_MISSING_DATA:表示语言的数据丢失。
     * TextToSpeech.LANG_NOT_SUPPORTED:不支持
     */
    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = textToSpeech.setLanguage(Locale.CHINA);
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (textToSpeech != null && !textToSpeech.isSpeaking()) {
            textToSpeech.setPitch(-1f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
            textToSpeech.speak(speechTxt.getText().toString(),
                    TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

加上权限:

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

布局文件:


<android.support.constraint.ConstraintLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入要朗诵的文字" />

        <Button
            android:id="@+id/btn_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读" />
    LinearLayout>

android.support.constraint.ConstraintLayout>

ok,接下来就可以用到项目中!
望君代码永无bug!!!!!!!!!!!!!!!!!!!

你可能感兴趣的:(java,android)