Java Code Examples for android.speech.tts.TextToSpeech#ERROR

The following examples show how to use android.speech.tts.TextToSpeech#ERROR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestingActivity.java    From ibm-wearables-android-sdk with Apache License 2.0 7 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing);

    detectedGesture = (TextView) findViewById(R.id.detectedGesture);
    score = (TextView) findViewById(R.id.scoring);

    detectedLayout = findViewById(R.id.detectedLayout);
    sensingLayout = findViewById(R.id.sensingLayout);

    getSupportActionBar().setTitle(R.string.testing_title);

    RecordApplication application = (RecordApplication) getApplication();
    controller = application.controller;


    textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.US);
            }
        }
    });
}
 
Example 2
Source File: TextSpeaker.java    From Chorus-RF-Laptimer with MIT License 6 votes vote down vote up
@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;
    }
}
 
Example 3
Source File: MediaController.java    From ankihelper with GNU General Public License v3.0 6 votes vote down vote up
public void setTextToSpeech(final Context context) {
    mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                mTextToSpeech.setLanguage(Locale.UK);
                mTextToSpeech.setSpeechRate(0.70f);
            }

            mTextToSpeech.setOnUtteranceCompletedListener(
                    new TextToSpeech.OnUtteranceCompletedListener() {
                        @Override
                        public void onUtteranceCompleted(String utteranceId) {
                            ((AppCompatActivity) context).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mIsSpeaking) {
                                        callbacks.highLightTTS();
                                    }
                                }
                            });
                        }
                    });
        }
    });
}
 
Example 4
Source File: Speech.java    From android-speech with Apache License 2.0 6 votes vote down vote up
@Override
public void onInit(final int status) {
    switch (status) {
        case TextToSpeech.SUCCESS:
            Logger.info(LOG_TAG, "TextToSpeech engine successfully started");
            break;

        case TextToSpeech.ERROR:
            Logger.error(LOG_TAG, "Error while initializing TextToSpeech engine!");
            break;

        default:
            Logger.error(LOG_TAG, "Unknown TextToSpeech status: " + status);
            break;
    }
}
 
Example 5
Source File: MainActivity.java    From Paideia with MIT License 6 votes vote down vote up
@Override
protected void onCreate(final Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

  setContentView(R.layout.activity_camera);

  tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
        if(status != TextToSpeech.ERROR) {
            tts.setLanguage(Locale.US);
        }
    }
  });
  if (null == savedInstanceState) {
    getFragmentManager()
        .beginTransaction()
        .replace(R.id.container, CameraConnectionFragment.newInstance())
        .commit();
  }
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.setStatusBarColor(baseColor);
}
 
Example 6
Source File: TtsActivity.java    From Android-Example with Apache License 2.0 5 votes vote down vote up
@Override
public void onInit(int status) {		
	if (status == TextToSpeech.SUCCESS) {
		Toast.makeText(TtsActivity.this, 
				"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
	}
	else if (status == TextToSpeech.ERROR) {
		Toast.makeText(TtsActivity.this, 
				"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
	}
}
 
Example 7
Source File: MainActivity.java    From Android-Example with Apache License 2.0 5 votes vote down vote up
@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(getApplicationContext(),
                "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
    }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(getApplicationContext(),
                "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
    }
}
 
Example 8
Source File: speechModule.java    From react-native-speech with MIT License 5 votes vote down vote up
public void init() {
    tts = new TextToSpeech(getReactApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.ERROR) {
                FLog.e(ReactConstants.TAG, "Not able to initialized the TTS object");
            }
        }
    });
}
 
Example 9
Source File: FailoverTextToSpeech.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to speak the specified text.
 *
 * @param text to speak, must be under 3999 chars.
 * @param locale language to speak with. Use default language if it's null.
 * @param pitch to speak text in.
 * @param rate to speak text in.
 * @param params to the TTS.
 * @return The result of speaking the specified text.
 */
private int trySpeak(
    CharSequence text,
    @Nullable Locale locale,
    float pitch,
    float rate,
    HashMap<String, String> params,
    int stream,
    float volume) {
  if (mTts == null) {
    return TextToSpeech.ERROR;
  }

  float effectivePitch = (pitch * mDefaultPitch);
  float effectiveRate = (rate * mDefaultRate);

  String utteranceId = params.get(Engine.KEY_PARAM_UTTERANCE_ID);
  if ((locale != null) && !locale.equals(mLastUtteranceLocale)) {
    if (attemptSetLanguage(locale)) {
      mLastUtteranceLocale = locale;
    }
  } else if ((locale == null) && (mLastUtteranceLocale != null)) {
    ensureSupportedLocale();
    mLastUtteranceLocale = null;
  }
  int result = speak(text, params, utteranceId, effectivePitch, effectiveRate, stream, volume);

  if (result != TextToSpeech.SUCCESS) {
    ensureSupportedLocale();
  }

  LogUtils.d(TAG, "Speak call for %s returned %d", utteranceId, result);
  return result;
}
 
Example 10
Source File: MainActivity.java    From android-apps with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etTxt=(EditText)findViewById(R.id.etTxt);
    btnPlay=(Button)findViewById(R.id.btnPlay);

    tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                tts.setLanguage(Locale.UK);
            }
        }
    });

    btnPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak = etTxt.getText().toString();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ttsGreater21(toSpeak);
            } else {
                ttsUnder20(toSpeak);
            }
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();

        }
    });

}
 
Example 11
Source File: InternalTextToSpeech.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
private void speak(final String message, final Locale loc, final int retries) {
  Log.d(LOG_TAG, "InternalTTS speak called, message = " + message);
  if (retries > ttsMaxRetries) {
    Log.d(LOG_TAG, "max number of speak retries exceeded: speak will fail");
    callback.onFailure();
  }
  // If speak was called before initialization was complete, we retry after a delay.
  // Keep track of the number of retries and fail if there are too many.
  if (isTtsInitialized) {
    Log.d(LOG_TAG, "TTS initialized after " + retries + " retries.");
    tts.setLanguage(loc);
    tts.setOnUtteranceCompletedListener(
        new TextToSpeech.OnUtteranceCompletedListener() {
          @Override
          public void onUtteranceCompleted(String utteranceId) {
            // onUtteranceCompleted is not called on the UI thread, so we use
            // Activity.runOnUiThread() to call callback.onSuccess().
            activity.runOnUiThread(new Runnable() {
              public void run() {
                callback.onSuccess();
              }
            });
          }
        });
    // We need to provide an utterance id. Otherwise onUtteranceCompleted won't be called.
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(nextUtteranceId++));
    int result = tts.speak(message, tts.QUEUE_FLUSH, params);
    if (result == TextToSpeech.ERROR) {
      Log.d(LOG_TAG, "speak called and tts.speak result was an error");
      callback.onFailure();
    }
  } else {
    Log.d(LOG_TAG, "speak called when TTS not initialized");
    mHandler.postDelayed(new Runnable() {
      public void run() {
        Log.d(LOG_TAG,
            "delaying call to speak.  Retries is: " + retries + " Message is: " + message);
        speak(message, loc, retries + 1);
      }
    }, ttsRetryDelay);
  }
}
 
Example 12
Source File: FailoverTextToSpeech.java    From talkback with Apache License 2.0 4 votes vote down vote up
/**
 * Speak the specified text.
 *
 * @param text The text to speak.
 * @param locale Language of the text.
 * @param pitch The pitch adjustment, in the range [0 ... 1].
 * @param rate The rate adjustment, in the range [0 ... 1].
 * @param params The parameters to pass to the text-to-speech engine.
 */
public void speak(
    CharSequence text,
    @Nullable Locale locale,
    float pitch,
    float rate,
    HashMap<String, String> params,
    int stream,
    float volume,
    boolean preventDeviceSleep) {

  String utteranceId = params.get(Engine.KEY_PARAM_UTTERANCE_ID);
  addRecentUtteranceId(utteranceId);

  // Handle empty text immediately.
  if (TextUtils.isEmpty(text)) {
    mHandler.onUtteranceCompleted(params.get(Engine.KEY_PARAM_UTTERANCE_ID), /* success= */ true);
    return;
  }

  int result;

  volume *= calculateVolumeAdjustment();

  if (preventDeviceSleep && mWakeLock != null && !mWakeLock.isHeld()) {
    mWakeLock.acquire();
  }

  Exception failureException = null;
  try {
    result = trySpeak(text, locale, pitch, rate, params, stream, volume);
  } catch (Exception e) {
    failureException = e;
    result = TextToSpeech.ERROR;
    allowDeviceSleep();
  }

  if (result == TextToSpeech.ERROR) {
    attemptTtsFailover(mTtsEngine);
  }

  if ((result != TextToSpeech.SUCCESS) && params.containsKey(Engine.KEY_PARAM_UTTERANCE_ID)) {
    if (failureException != null) {
      LogUtils.w(TAG, "Failed to speak %s due to an exception", text);
      failureException.printStackTrace();
    } else {
      LogUtils.w(TAG, "Failed to speak %s", text);
    }

    mHandler.onUtteranceCompleted(params.get(Engine.KEY_PARAM_UTTERANCE_ID), /* success= */ true);
  }
}
 
Example 13
Source File: PlumbleService.java    From Plumble with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onInit(int status) {
    if(status == TextToSpeech.ERROR)
        logWarning(getString(R.string.tts_failed));
}