使用TextToSpeech语音读取

    xiaoxiao2021-04-11  35

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

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

    内嵌的类:

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

    常量:

            类型                                             名称                       功能StringACTION_TTS_QUEUE_PROCESSING_COMPLETED广播事件,表示TextToSpeech转化器已经转化未所有处于语音队列的文本intERROR表示一般的操作失败intERROR_INVALID_REQUEST表示由于无效请求导致的失败intERROR_NETWORK表示由于网络连接问题导致的失败intERROR_NETWORK_TIMEOUT表示由于网络连接超时引起的失败intERROR_NOT_INSTALLED_YET表示由于未完成的语音数据下载导致的错误intERROR_OUTPUT表示输出产生的失败intERROR_SERVICE表示由于TTS服务产生的失败intERROR_SYNTHESIS表示由于引擎的输入转化的输入内容引起的失败`intLANG_AVAILABLE表示本地语言可用,但不是方言或者引申语言(不知道对不对)intLANG_COUNTRY_AVAILABLE表示本地语音或者方言可用,引申语音不可用intLANG_COUNTRY_VAR_AVAILABLE表示本地语音可用intLANG_MISSING_DATA语言包丢失intLANG_NOT_SUPPORTED表示语音不支持intQUEUE_ADD新的转化任务添加到队列后面intQUEUE_FLUSH新的任务替代以前的任务,直接中断以前的任务intSTOPPED表示由代理要求的停止intSUCCESS操作成功

    构造方法:

    //使用默认的引擎 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.javapackage 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(); } } }

    转载请注明原文地址: https://ju.6miu.com/read-666470.html

    最新回复(0)