100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android 文字转语音(中文) TextToSpeech+科大讯飞语音引擎3.0

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

时间:2018-11-23 10:55:38

相关推荐

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

最近项目中需要用到文字转语音。

本来是想使用朗读女生成的声音文件放到项目资源中进行播放。

但是产品要求改成动态的。于是就用了Google为我们封装好的类TTS,即[TextToSpeech]:大家可以看下详细文档。

代码其实不多,但是写完之后测试就有问题,没声音,,,,看了之后才知道谷歌这个官方api不支持中文。。很Tmd.

给大家科普下:

文字转语音的引擎:

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

引擎下载地址:

/download/sqq_yj/10649462

-

需要再手机设置中修改:

语言和输入法 ==》 文字转语音(TTS)输出 ==》首选引擎由默认的Pico TTS 改为 科大讯飞语音引擎3.0

接下来就是代码:

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对象@Overridepublic 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}@Overrideprotected void onStop() {super.onStop();textToSpeech.stop(); // 不管是否正在朗读TTS都被打断textToSpeech.shutdown(); // 关闭,释放资源}/*** 用来初始化TextToSpeech引擎* status:SUCCESS或ERROR这2个值* setLanguage设置语言,帮助文档里面写了有22种* TextToSpeech.LANG_MISSING_DATA:表示语言的数据丢失。* TextToSpeech.LANG_NOT_SUPPORTED:不支持*/@Overridepublic 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();}}}@Overridepublic 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"/>

布局文件:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/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!!!!!!!!!!!!!!!!!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。