您的位置:首页 > 其它

使用TextToSpeech语音读取

2017-04-13 14:30 429 查看
今天无意见发现Android中有个控件可以实现语音读取,它就是TextToSpeech控件。目前只支持5种语言:English、 French 、 German 、 Italian 和 Spanish。(很遗憾没有
中文)。这个控件的好处是首先不要权限。


TextToSpeech
必须再被实例化之后才能使用.实现
TextToSpeech.OnInitListener
方法来获取实例化结果的提醒。当你已经使用完
TextToSpeech
实例之后,
应该调用
shutdown()
方法来释放
TextToSpeech
所使用的本地资源。



内嵌的类:

     类型                                   名称                  功能
class
TextToSpeech.Engine
控制文字转化语音的常亮或者参数名
class
TextToSpeech.EngineInfo
已安装的语音引擎的信息
interface
TextToSpeech.OnInitListener
定义了语音引擎初始化结果的回调接口
interface
TextToSpeech.OnUtteranceCompletedListener
API level 18被弃用 . 使用
UtteranceProgressListener
替代


常量:

        类型                                             名称                       功能
String
ACTION_TTS_QUEUE_PROCESSING_COMPLETED
广播事件,表示
TextToSpeech
转化器已经转化未所有处于语音队列的文本
int
ERROR
表示一般的操作失败
int
ERROR_INVALID_REQUEST
表示由于无效请求导致的失败
int
ERROR_NETWORK
表示由于网络连接问题导致的失败
int
ERROR_NETWORK_TIMEOUT
表示由于网络连接超时引起的失败
int
ERROR_NOT_INSTALLED_YET
表示由于未完成的语音数据下载导致的错误
int
ERROR_OUTPUT
表示输出产生的失败
int
ERROR_SERVICE
表示由于TTS服务产生的失败
int
ERROR_SYNTHESIS
表示由于引擎的输入转化的输入内容引起的失败
`int
LANG_AVAILABLE
表示本地语言可用,但不是方言或者引申语言(不知道对不对)
int
LANG_COUNTRY_AVAILABLE
表示本地语音或者方言可用,引申语音不可用
int
LANG_COUNTRY_VAR_AVAILABLE
表示本地语音可用
int
LANG_MISSING_DATA
语言包丢失
int
LANG_NOT_SUPPORTED
表示语音不支持
int
QUEUE_ADD
新的转化任务添加到队列后面
int
QUEUE_FLUSH
新的任务替代以前的任务,直接中断以前的任务
int
STOPPED
表示由代理要求的停止
int
SUCCESS
操作成功




构造方法:

//使用默认的引擎
TextToSpeech(Context context, TextToSpeech.OnInitListener listener)
//使用指定的引擎
TextToSpeech(Context context, TextToSpeech.OnInitListener listener, String engine)




Demo

布局文件:

activity_speech.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testtexttospeech.TestTextToSpeechActivity">

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

<EditText
android:id="@+id/edview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容..."
android:textSize="16dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>

<Button
android:id="@+id/speechBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SpeechButton"/>

</LinearLayout>

</RelativeLayout>
代码:
TestTextSpeechActivity.java
package com.example.testtexttospeech;

import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class TestTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener{

private Button speechBtn;
private EditText edview;
private TextToSpeech textToSpeech;

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

textToSpeech = new TextToSpeech(this,this);

edview = (EditText) findViewById(R.id.edview);
speechBtn = (Button) findViewById(R.id.speechBtn);
speechBtn.setEnabled(false);
speechBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String contextStr = edview.getText().toString().trim();
if(TextUtils.isEmpty( contextStr )){
Toast.makeText(TestTextToSpeechActivity.this, "please input context",Toast.LENGTH_SHORT).show();
return;
}
initSpeech(contextStr);
}
});
}

@Override
public void onInit(int status) {
if( status == TextToSpeech.SUCCESS){
int result = textToSpeech.setLanguage(Locale.US);
if( result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
speechBtn.setEnabled(false);
Log.e("TAG","Language is not available");
}else{
//TTS引擎已经成功初始化
speechBtn.setEnabled(true);
}
}else{
// 初始化失败
Log.e( "TAG", "Could not initialize TextToSpeech.");
}
}

private void initSpeech( String contextStr ){
if( textToSpeech != null && !textToSpeech.isSpeaking()){
textToSpeech.setPitch(0.5f); // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.speak( contextStr ,TextToSpeech.QUEUE_FLUSH, null);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if( textToSpeech != null ){
// 停止TextToSpeech
textToSpeech.stop();
//释放 TextToSpeech占用的资源
textToSpeech.shutdown();
}
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息