tts speak
refer to sayNotiText.apk
void initiateTTS(Context context) {
mContext = context;
mTTS = null;
mTTS = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTTS.setLanguage(Locale.KOREA);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
utils.logE(logID, "Language not supported");
}
} else {
utils.logE(logID, "Initialization failed");
}
}
});
if (ttsPitch == 0f) {
ttsPitch = 1.2f;
ttsSpeed = 1.4f;
}
}
void setPitch(float p) {
ttsPitch = p;
}
void setSpeed(float s) {
ttsSpeed = s;
}
void speak(String text) {
String match = "[^\uAC00-\uD7A3xfe0-9a-zA-Z./,\\s]"; // 한글, 영문, 숫자만 OK
// String match = "[`~!@#$%^&*()'/+;<>\\_▶★]"; // 특수문자 읽기 방지
text = text.replace("ㅎ","흐.")
.replace("ㅋ","크.")
.replace("ㅠ","유.")
.replaceAll(match, " ");
try {
mAudioManager.requestAudioFocus(mFocusGain);
} catch (Exception e) {
utils.logE(logID, "requestAudioFocus");
}
ttsSpeak(text);
long delayTime = (long) ((float) (text.length() * 240) / ttsSpeed);
new Timer().schedule(new TimerTask() {
public void run() {
mAudioManager.abandonAudioFocusRequest(mFocusGain);
}
}, delayTime);
}
private void ttsSpeak(String text) {
readyAudioTTS();
try {
mTTS.speak(text, TextToSpeech.QUEUE_ADD, null, TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID);
} catch (Exception e) {
utils.logE(logID, "justSpeak:" + e.toString());
}
}
void ttsStop() {
mTTS.stop();
mAudioManager.abandonAudioFocusRequest(mFocusGain);
try {
readyAudioTTS();
} catch (Exception e) {
utils.logE(logID, "ttsStop:" + e.toString());
}
}
void readyAudioTTS() {
utils.readyAudioManager(mContext);
if (ttsPitch == 0f) {
ttsPitch = 1.2f;
ttsSpeed = 1.4f;
}
if (mTTS == null) {
utils.log(logID, "$$ mTTS NULL ");
initiateTTS(mContext);
}
mTTS.setPitch(ttsPitch);
mTTS.setSpeechRate(ttsSpeed);
}
void readyAudioManager(Context context) {
if(mAudioManager == null) {
mContext = context;
try {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mFocusGain = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
.build();
} catch (Exception e) {
append2file("timestamp.txt", "mAudioManager Error " + e.toString());
}
}
}
'android source ' 카테고리의 다른 글
Horizontal Wheel (0) | 2019.09.06 |
---|---|
Customized AlertDialog (0) | 2019.09.06 |
mp3 media play (0) | 2019.08.17 |
To get the Camera full folder name (0) | 2019.08.17 |
read text file in raw folder (0) | 2019.08.17 |