TTS语音合成技术-进阶

1. TextToSpeechUtil2类

package comi.example.liy.firstbasicproject.tool;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

import java.util.Locale;

import comi.example.liy.firstbasicproject.R;

/**
 * Created by liy on 2018-07-11.
 */

public class TextToSpeechUtil2 {

    private static TextToSpeech textToSpeech;

    //获取TextToSpeech对象并设置相关属性
    public static void speak(final Context context, final String text){
        if(textToSpeech == null){
            textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status == TextToSpeech.SUCCESS) {//装载TTS引擎成功
                        //Locale.getDefault()获取系统默认的区域信息:如系统语言设置为中文则参数为 "zho","CHN",设置为美式英语则参数为 "eng","USA"
                        int result = textToSpeech.setLanguage(new Locale(Locale.getDefault().getISO3Language(),Locale.getDefault().getISO3Country()));
                        Log.v("ttSpeech_result",result+","+ Locale.getDefault().getISO3Language() + ","+ Locale.getDefault().getISO3Country() + "");//如果打印为-2,说明不支持这种语言
                        if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                            Toast.makeText(context,context.getString(R.string.missing_or_unsupported), Toast.LENGTH_SHORT).show();
                        }else {
                            textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
                            textToSpeech.setSpeechRate(1.0f);// 设置语速
                            textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH, null);//开始阅读
                        }
                    }else {
                        Toast.makeText(context,context.getString(R.string.load_tts_fail), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }else {
            //text:阅读的文字    
            //TextToSpeech.QUEUE_ADD:添加到队列后面,依次将前面的读完轮序
            //TextToSpeech.QUEUE_FLUSH:刷新队列,将之前的队列取消阅读现在的文字
            textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH, null);//开始阅读
        }

    }

    //释放TextToSpeech对象
    public static void shutdown(){
        if (textToSpeech != null) {
            textToSpeech.shutdown();
        }

    }
}

2. HomeActivity

public class HomeActivity extends Activity implements ActivityGeneralInterface {
    private Button btnTextToSpeech2;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_home);
        initViews();
        initData();
        initListeners();
    }

    @Override
    public void initViews() {
        btnTextToSpeech2 = (Button)findViewById(R.id.activity_main_textToSpeech2);
    }

    @Override
    public void initData() {
        
    }

    @Override
    public void initListeners() {
        
    }

    public void TextToSpeechOnClick2(View view){
        TextToSpeechUtil2.speak(this,getString(R.string.text_to_speech));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        textToSpeechUtil.shutdown();
    } 
  1. ActivityGeneralInterface接口
public interface ActivityGeneralInterface {

    void initViews();
    void initData();
    void initListeners();
}
  1. activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
          android:id="@+id/activity_main_textToSpeech2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="TextToSpeech2"
          android:textAllCaps="false"
          android:onClick="TextToSpeech2OnClick"/>
</LinearLayout>

5、资源文件string.xml

 <string name="text_to_speech">文字转语音</string>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,081评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,085评论 2 59
  • 有一个问题想提问我心里明白,如果不是跟你一样价值观的人,不要跟他提共同成长这件事。因为我的一个同窗好友,现在感情还...
    品兴阅读 243评论 0 0
  • “来,看这里,这里……看那里,宝宝,快看……” 一个妈妈正卖力的拿着两个大红毛线球吸引孩子的视线,孩子却哇哇的哭起...
    杨杨育儿说阅读 351评论 0 0
  • 1、中秋一过,江南的天气一天凉似一天,晩饭时喝了一点酒,微醺,见窗外天空中飘起了小雨,天色将黑未黑,眼前一派如烟似...
    江边鹤阅读 693评论 0 1