0
点赞
收藏
分享

微信扫一扫

语音合成

前程有光 2023-02-22 阅读 89


/**
* Created by Administrator on 2019/5/9 0009.
*/


import android.content.Context;
import android.speech.tts.TextToSpeech;


import java.util.Locale;

/**
* Created by zhenqiang on 2016/12/9.
*/

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.CHINESE);
textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.setSpeechRate(1.0f);
}
}
});
}

public void speakText(String text) {
if (textToSpeech != null) {
textToSpeech.speak(text,
TextToSpeech.QUEUE_FLUSH, null);
}

}

}


举报

相关推荐

0 条评论