/**
   * Note: we enforce that this method is called on the UI thread, so we can call
   * nativeVoicesChanged directly.
   */
  private void initialize() {
    assert mNativeTtsPlatformImplAndroid != 0;

    // Note: Android supports multiple speech engines, but querying the
    // metadata about all of them is expensive. So we deliberately only
    // support the default speech engine, and expose the different
    // supported languages for the default engine as different voices.
    String defaultEngineName = mTextToSpeech.getDefaultEngine();
    String engineLabel = defaultEngineName;
    for (TextToSpeech.EngineInfo info : mTextToSpeech.getEngines()) {
      if (info.name.equals(defaultEngineName)) engineLabel = info.label;
    }
    Locale[] locales = Locale.getAvailableLocales();
    mVoices = new ArrayList<TtsVoice>();
    for (int i = 0; i < locales.length; ++i) {
      if (!locales[i].getVariant().isEmpty()) continue;
      if (mTextToSpeech.isLanguageAvailable(locales[i]) > 0) {
        String name = locales[i].getDisplayLanguage();
        if (!locales[i].getCountry().isEmpty()) {
          name += " " + locales[i].getDisplayCountry();
        }
        TtsVoice voice = new TtsVoice(name, locales[i].toString());
        mVoices.add(voice);
      }
    }

    mInitialized = true;
    nativeVoicesChanged(mNativeTtsPlatformImplAndroid);
  }
  @Override
  public void onInit(int status) {
    int result;
    if (status == TextToSpeech.SUCCESS) {
      Locale[] AvalLoc = Locale.getAvailableLocales();

      Log.i("TTS", "Available locales " + Arrays.toString(AvalLoc));

      Locale pol_loc = new Locale("en", "EN");
      if (TextToSpeech.LANG_AVAILABLE == tts.isLanguageAvailable(pol_loc)) {
        result = tts.setLanguage(pol_loc);
      } else {
        result = tts.setLanguage(Locale.ITALIAN);
      }

      if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e("TTS", "LANG_NOT_SUPPORTED");
      } else {
        btnSpeak.setEnabled(true);
        speakOut();
      }

    } else {
      Log.e("TTS", "Initialization Failed");
    }
  }
Ejemplo n.º 3
0
        @Override
        public void onInit(int status) {
          // TODO Auto-generated method stub
          Locale loc = new Locale("uk");
          SharedPreferences setting =
              getSharedPreferences("wordroid.model_preferences", MODE_PRIVATE);
          if (setting.getString("category", "1").equals("2")) loc = new Locale("us");

          /* 检查是否支持输入的时区 */
          if (tts.isLanguageAvailable(loc) == TextToSpeech.LANG_AVAILABLE) {
            /* 设定语言 */
            tts.setLanguage(loc);
          }
          tts.setOnUtteranceCompletedListener(ttsUtteranceCompletedListener);
        }
Ejemplo n.º 4
0
 public static void buildAvailableLanguages() {
   availableTtsLocales.clear();
   Locale[] systemLocales = Locale.getAvailableLocales();
   for (Locale loc : systemLocales) {
     try {
       int retCode = mTts.isLanguageAvailable(loc);
       if (retCode >= TextToSpeech.LANG_COUNTRY_AVAILABLE) {
         availableTtsLocales.add(loc);
       } else {
         Timber.v(
             "ReadText.buildAvailableLanguages() :: %s  not available (error code %d)",
             loc.getDisplayName(), retCode);
       }
     } catch (IllegalArgumentException e) {
       Timber.e("Error checking if language " + loc.getDisplayName() + " available");
     }
   }
 }
  /** Called when TTS is ready. */
  protected void onTtsReady() {
    Locale locale = Locale.getDefault();
    int languageAvailability = tts.isLanguageAvailable(locale);
    if (languageAvailability == TextToSpeech.LANG_MISSING_DATA
        || languageAvailability == TextToSpeech.LANG_NOT_SUPPORTED) {
      Log.w(TAG, "Default locale not available, use English.");
      locale = Locale.ENGLISH;
      /*
       * TODO: instead of using english, load the language if missing and show a
       * toast if not supported. Not able to change the resource strings to
       * English.
       */
    }
    tts.setLanguage(locale);

    // Slow down the speed just a bit as it is hard to hear when exercising.
    tts.setSpeechRate(TTS_SPEECH_RATE);
  }
Ejemplo n.º 6
0
  protected void execute(Command cmd) {
    Log.i("TTS: " + cmd.getOriginalCommand());

    if (isMatchingCmd(cmd, "tts")) {
      if (mTtsAvailable) {
        mTts.speak(cmd.getAllArg1(), TextToSpeech.QUEUE_ADD, null);
      } else {
        send(R.string.chat_tts_installation);
        Intent intent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        sContext.startActivity(intent);
      }
    } else if (isMatchingCmd(cmd, "tts-engine-list")) {
      if (Build.VERSION.SDK_INT < VERSION_CODES.ICE_CREAM_SANDWICH) {
        send(R.string.android_version_incompatible, "ICE CREAM SANDWICH");
      } else {
        StringBuilder sb = new StringBuilder(getString(R.string.chat_tts_engines));
        for (EngineInfo engine : mTts.getEngines()) {
          sb.append(engine.label).append(" - ").append(engine.name).append("\n");
        }
        send(sb.substring(0, Math.max(0, sb.length() - 1)));
      }
    } else if (isMatchingCmd(cmd, "tts-lang-list")) {
      StringBuilder sb = new StringBuilder(getString(R.string.chat_tts_languages));
      for (Locale locale : Locale.getAvailableLocales()) {
        switch (mTts.isLanguageAvailable(locale)) {
          case TextToSpeech.LANG_AVAILABLE:
            sb.append(locale.getDisplayLanguage());
            if (locale.getDisplayCountry() != null && locale.getDisplayCountry().length() > 0) {
              sb.append(" (").append(locale.getDisplayCountry()).append(")");
            }
            sb.append(" - ").append(locale.getLanguage());

            if (locale.getDisplayCountry() != null && locale.getDisplayCountry().length() > 0) {
              sb.append(":").append(locale.getCountry());
            }
            sb.append("\n");
            break;
        }
      }
      send(sb.substring(0, Math.max(0, sb.length() - 1)));
    } else if (isMatchingCmd(cmd, "tts-engine")) {
      if (Build.VERSION.SDK_INT < VERSION_CODES.ICE_CREAM_SANDWICH) {
        send(R.string.android_version_incompatible, "ICE CREAM SANDWICH");
      } else {
        mTts = new TextToSpeech(sContext, this, cmd.getAllArg1());
        send(getString(R.string.chat_tts_engine) + mTts.getDefaultEngine());
      }
    } else if (isMatchingCmd(cmd, "tts-lang")) {
      String arg1 = cmd.getArg1();
      String arg2 = cmd.getArg2();
      if (!arg1.equals("") && !arg2.equals("")) {
        mLocale = new Locale(arg1, arg2);
        mTts.setLanguage(mLocale);
      } else if (!arg1.equals("")) {
        mLocale = new Locale(arg1);
        mTts.setLanguage(mLocale);
      }
      send(getString(R.string.chat_tts_language) + mTts.getLanguage().getDisplayName());
    }
  }