本文整理汇总了Java中android.speech.tts.TextToSpeech类的典型用法代码示例。如果您正苦于以下问题:Java TextToSpeech类的具体用法?Java TextToSpeech怎么用?Java TextToSpeech使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TextToSpeech类属于android.speech.tts包,在下文中一共展示了TextToSpeech类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Init
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
public void Init() {
if (_ttsInitialized) {
Logger.getInstance().Warning(TAG, "Already initialized!");
return;
}
_ttsSpeaker = new TextToSpeech(_context, status -> {
if (status == TextToSpeech.SUCCESS) {
int result = _ttsSpeaker.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Logger.getInstance().Error(TAG, "This Language is not supported!");
} else {
_receiverController.RegisterReceiver(_speakReceiver, new String[]{TTS_SPEAK_TEXT_BROADCAST});
_ttsInitialized = true;
}
} else {
Logger.getInstance().Error(TAG, "Initialization failed!");
}
});
}
示例2: onInit
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
//int result= tts.setLanguage(Locale.ENGLISH);
Locale locale = new Locale("tr", "TR");
int result = tts.setLanguage(locale);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
btn_konus.setEnabled(true);
String text = editText1.getText().toString();
speakOut(text);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
示例3: init
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
public void init(Context ctx) {
prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
prefsÆndret();
tts = new TextToSpeech(ctx, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
initialiseret = true;
int res = tts.setLanguage(new Locale("da", ""));
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED) {
res = tts.setLanguage(Locale.getDefault());
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED) {
res = tts.setLanguage(Locale.US);
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED) {
initialiseret = false;
}
}
}
// udtal("Tekst til tale initialiseret for sproget " + tts.getLanguage().getDisplayLanguage(tts.getLanguage()));
}
}
});
}
示例4: getSpeechFromText
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
/**
* Create a new audio recording based on text passed in, update the callback with the changing states
* @param text the text to render
* @param callback
*/
public void getSpeechFromText(String text, SpeechFromTextCallback callback){
//create a new unique ID
String utteranceId = AuthorizationManager.createCodeVerifier();
//add the callback to our list of callbacks
mCallbacks.put(utteranceId, callback);
//get our TextToSpeech engine
TextToSpeech textToSpeech = getTextToSpeech();
//set up our arguments
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
//request an update from TTS
textToSpeech.synthesizeToFile(text, params, getCacheFile(utteranceId).toString());
}
示例5: getAvailableLocales
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
@ReactMethod
public void getAvailableLocales(Promise promise) {
if(notReady(promise)) return;
try {
WritableArray localeList = Arguments.createArray();
Locale[] localesArray = Locale.getAvailableLocales();
for(Locale locale: localesArray) {
int isAvailable = tts.isLanguageAvailable(locale);
if(isAvailable == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
WritableMap newLocale = returnMapForLocale(locale);
localeList.pushMap(newLocale);
}
}
promise.resolve(localeList);
} catch(Exception e) {
promise.reject("error", "Unable to retrieve locales for getAvailableLocales()", e);
}
}
开发者ID:echo8795,项目名称:react-native-android-text-to-speech,代码行数:21,代码来源:RNAndroidTextToSpeechModule.java
示例6: searchVoice
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
public static Voice searchVoice(String voiceName, TextToSpeech tts) {
if (MainActivity.isLoggingEnabled)
Log.i("VOICES:", tts.getVoices().toString()); //stampa tutte le voci disponibili
for (Voice tmpVoice : tts.getVoices()) {
if (tmpVoice.getName().equals(voiceName)) {
return tmpVoice;
}
}
return null;
}
示例7: speakResults
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
public void speakResults(TextToSpeech tts, List<Recognition> results) {
if (results.isEmpty()) {
tts.speak("I don't understand what I see.", TextToSpeech.QUEUE_ADD, null, UTTERANCE_ID);
if (isFeelingFunnyNow()) {
tts.speak("Please don't unplug me, I'll do better next time.",
TextToSpeech.QUEUE_ADD, null, UTTERANCE_ID);
}
} else {
if (isFeelingFunnyNow()) {
playJoke(tts);
}
if (results.size() == 1
|| results.get(0).getConfidence() > SINGLE_ANSWER_CONFIDENCE_THRESHOLD) {
tts.speak(String.format(Locale.getDefault(),
"I see a %s", results.get(0).getTitle()),
TextToSpeech.QUEUE_ADD, null, UTTERANCE_ID);
} else {
tts.speak(String.format(Locale.getDefault(), "This is a %s, or maybe a %s",
results.get(0).getTitle(), results.get(1).getTitle()),
TextToSpeech.QUEUE_ADD, null, UTTERANCE_ID);
}
}
}
示例8: playJoke
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
private boolean playJoke(TextToSpeech tts) {
long now = System.currentTimeMillis();
// choose a random joke whose last occurrence was far enough in the past
SortedMap<Long, Utterance> availableJokes = mJokes.headMap(now - JOKE_COOLDOWN_MILLIS);
Utterance joke = null;
if (!availableJokes.isEmpty()) {
int r = RANDOM.nextInt(availableJokes.size());
int i = 0;
for (Long key : availableJokes.keySet()) {
if (i++ == r) {
joke = availableJokes.remove(key); // also removes from mJokes
break;
}
}
}
if (joke != null) {
joke.speak(tts);
// add it back with the current time
mJokes.put(now, joke);
return true;
}
return false;
}
示例9: speak
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
@SuppressWarnings("deprecation")
private void speak(String text) {
boolean ttsEnabled = preferences.getBoolean("tts_enabled", true);
if (ttsEnabled) {
speakingDialog.show();
// Stop listening so it doesn't trigger itself.
if (hotwordDetector != null) {
hotwordDetector.stopListening();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, "messageID");
} else {
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID");
tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
}
}
示例10: onInit
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
Locale localeToSet = Locale.US;
Locale current = Locale.getDefault();
if (!useEnglishOnly && SUPPORTED_LOCALES.containsKey(current.getLanguage())) {
localeToSet = current;
}
Configuration currentConfig = originalContext.getResources().getConfiguration();
if (!currentConfig.locale.equals(localeToSet)) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
setContextToUseLegacy(currentConfig, localeToSet);
} else {
setContextToUse(currentConfig, localeToSet);
}
}
tts.setLanguage(localeToSet);
DecimalFormatSymbols dfs = new DecimalFormatSymbols(localeToSet);
decimalSeparator = Character.toString(dfs.getDecimalSeparator());
isInitialized = true;
}
}
示例11: audioEnroll
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
/**
* All is well - begin the audio enrollment
*/
private void audioEnroll(@NonNull final String profileId) {
if (DEBUG) {
MyLog.i(CLS_NAME, "audioEnroll");
}
final String utterance;
if (SPH.getEnrollmentVerbose(getApplicationContext())) {
utterance = getApplicationContext().getString(R.string.speech_enroll_instructions_40);
} else {
SPH.setEnrollmentVerbose(getApplicationContext());
utterance = getApplicationContext().getString(R.string.speech_enroll_instructions_first);
}
final LocalRequest request = new LocalRequest(getApplicationContext());
request.prepareDefault(LocalRequest.ACTION_SPEAK_LISTEN, utterance);
request.setQueueType(TextToSpeech.QUEUE_ADD);
request.setCondition(Condition.CONDITION_IDENTITY);
request.setIdentityProfile(profileId);
request.execute();
}
示例12: createTTS
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
private void createTTS() {
//Creates an instance of Google API TTS
mTTS = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//If initializaation is sucessful so set language.
if (status == TextToSpeech.SUCCESS) {
//Set language to PT-BR and get the value returned
int result = mTTS.setLanguage(new Locale("pt", "br"));
//Check if the language is supported, if not print it on Android Monitor.
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TextToSpeechManager", "This Language is not supported");
}
} else {
Log.e("TextToSpeechManager", "Initilization Failed!");
}
}
});
}
示例13: TtsPlatformImpl
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
protected TtsPlatformImpl(long nativeTtsPlatformImplAndroid, Context context) {
mInitialized = false;
mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid;
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
initialize();
}
});
}
}
});
addOnUtteranceProgressListener();
}
示例14: speak
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
/**
* Attempt to start speaking an utterance. If it returns true, will call back on
* start and end.
*
* @param utteranceId A unique id for this utterance so that callbacks can be tied
* to a particular utterance.
* @param text The text to speak.
* @param lang The language code for the text (e.g., "en-US").
* @param rate The speech rate, in the units expected by Android TextToSpeech.
* @param pitch The speech pitch, in the units expected by Android TextToSpeech.
* @param volume The speech volume, in the units expected by Android TextToSpeech.
* @return true on success.
*/
@CalledByNative
private boolean speak(int utteranceId, String text, String lang,
float rate, float pitch, float volume) {
if (!mInitialized) {
mPendingUtterance = new PendingUtterance(this, utteranceId, text, lang, rate,
pitch, volume);
return true;
}
if (mPendingUtterance != null) mPendingUtterance = null;
if (!lang.equals(mCurrentLanguage)) {
mTextToSpeech.setLanguage(new Locale(lang));
mCurrentLanguage = lang;
}
mTextToSpeech.setSpeechRate(rate);
mTextToSpeech.setPitch(pitch);
int result = callSpeak(text, volume, utteranceId);
return (result == TextToSpeech.SUCCESS);
}
示例15: onInit
import android.speech.tts.TextToSpeech; //导入依赖的package包/类
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.getDefault());
isInit = true;
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("error", "This Language is not supported");
}
Log.d("TextToSpeech", "Initialization Suceeded! " + System.currentTimeMillis());
speak(mContext.getString(R.string.hello1) + ",");
} else {
Log.e("error", "Initialization Failed! " + status);
}
}