package com.megvii.idcardo2o.util; /** * 针对英文 */ import android.content.Context; import android.speech.tts.TextToSpeech; import java.util.Locale; public class SpeechUtils { private Context context; private static final String TAG = "SpeechUtils"; private static SpeechUtils singleton; private TextToSpeech textToSpeech; // TTS对象 public static SpeechUtils getInstance(Context context) { if (singleton == null) { synchronized (SpeechUtils.class) { if (singleton == null) { singleton = new SpeechUtils(context); } } } return singleton; } public SpeechUtils(Context context) { this.context = context; textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() { @Override public void onInit(int i) { if (i == TextToSpeech.SUCCESS) { textToSpeech.setLanguage(Locale.US); // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规 textToSpeech.setPitch(1.0f); textToSpeech.setSpeechRate(1.0f); } } }); } public void speakText(String text) { if (textToSpeech != null) { textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); } } }
调用:SpeechUtils.getInstance(getApplicationContext()).speakText("hello world");
注意:此类只针对英文的播报!!!