本文整理匯總了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);
}
}