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

The following examples show how to use android.speech.tts.TextToSpeech#OnInitListener . 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: 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 2
Source File: Accessibility.java    From OsmGo with MIT License 6 votes vote down vote up
@PluginMethod()
public void speak(PluginCall call) {
  final String value = call.getString("value");
  final String language = call.getString("language", "en");
  final Locale locale = Locale.forLanguageTag(language);

  if (locale == null) {
    call.error("Language was not a valid language tag.");
    return;
  }

  tts = new TextToSpeech(getContext(), new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int i) {
      tts.setLanguage(locale);
      tts.speak(value, TextToSpeech.QUEUE_FLUSH, null, "capacitoraccessibility" + System.currentTimeMillis());
    }
  });

  // Not yet implemented
  throw new UnsupportedOperationException();
}
 
Example 3
Source File: CompassService.java    From PTVGlass with MIT License 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // Even though the text-to-speech engine is only used in response to a menu action, we
    // initialize it when the application starts so that we avoid delays that could occur
    // if we waited until it was needed to start it up.
    mSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            // Do nothing.
        }
    });

    SensorManager sensorManager =
            (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    LocationManager locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mOrientationManager = new OrientationManager(sensorManager, locationManager);
    mLandmarks = new Landmarks(this);
}
 
Example 4
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 5
Source File: TtsPlatformImpl.java    From delion with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 6
Source File: Speech.java    From 10000sentences with Apache License 2.0 6 votes vote down vote up
public Speech(Context context, Language language) {
    this.context = context;
    this.locale = findLocale(language);
    this.languageFound = locale != null;
    this.enabled = Preferences.isUseTTS(context);
    if (!this.enabled) {
        return;
    }
    tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {
            Speech.this.initialized = true;
        }
    });
    tts.setPitch(1);
    tts.setSpeechRate(0.75F);
    tts.setLanguage(locale);
}
 
Example 7
Source File: SpeedHudService.java    From SpeedHud with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // Even though the text-to-speech engine is only used in response to a menu action, we
    // initialize it when the application starts so that we avoid delays that could occur
    // if we waited until it was needed to start it up.
    mSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            // Do nothing.
        }
    });

    SensorManager sensorManager =
            (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    LocationManager locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mOrientationManager = new OrientationManager(sensorManager, locationManager);
}
 
Example 8
Source File: LiteracyApplication.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
@Override
    public void onCreate() {
        Log.i(getClass().getName(), "onCreate");
        super.onCreate();

        // Check if the application's versionCode was upgraded
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int oldVersionCode = sharedPreferences.getInt(PREF_APP_VERSION_CODE, 0);
        int newVersionCode = VersionHelper.getAppVersionCode(getApplicationContext());
        if (oldVersionCode == 0) {
            sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, newVersionCode).commit();
            oldVersionCode = newVersionCode;
        }
        if (oldVersionCode < newVersionCode) {
            Log.i(getClass().getName(), "Upgrading application from version " + oldVersionCode + " to " + newVersionCode);
//            if (oldVersionCode < ???) {
//                // Put relevant tasks required for upgrading here
//            }
            sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, newVersionCode).commit();
        }

        // Initialize TTS
        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                Log.i(getClass().getName(), "TextToSpeech onInit");
                Log.i(getClass().getName(), "TextToSpeech status: " + status);
            }
        });
    }
 
Example 9
Source File: AndroidSpeechPlayer.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * Creates an instance of {@link AndroidSpeechPlayer}.
 *
 * @param context  used to create an instance of {@link TextToSpeech}
 * @param language to initialize locale to set
 * @since 0.6.0
 */
AndroidSpeechPlayer(Context context, final String language, final SpeechListener speechListener) {
  textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
      boolean ableToInitialize = status == TextToSpeech.SUCCESS && language != null;
      if (!ableToInitialize) {
        Timber.e("There was an error initializing native TTS");
        return;
      }
      setSpeechListener(speechListener);
      initializeWithLanguage(new Locale(language));
    }
  });
}
 
Example 10
Source File: BgToSpeech.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
private BgToSpeech(Context context){
    this.context = context;
    this.tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {

            Log.d("BgToSpeech", "Calling onInit(), tts = " + tts);
            if (status == TextToSpeech.SUCCESS && tts != null) {
                
                //try local language
                Locale loc = Locale.getDefault();
                Log.d("BgToSpeech", "status == TextToSpeech.SUCCESS + loc" + loc);
                int result = tts.setLanguage(Locale.getDefault());
                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("BgToSpeech", "Default system language is not supported");
                    result = tts.setLanguage(Locale.ENGLISH);
                }
                //try any english
                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("BgToSpeech", "English is not supported");
                    tts = null;
                }
            } else {
                Log.e("BgToSpeech", "status != TextToSpeech.SUCCESS; status: " + status);
                tts= null;
            }
        }
    });
}
 
Example 11
Source File: TtsProvider.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public TtsProvider(Context context, TextToSpeech.OnInitListener listener) {
    // TODO: use the 3-arg constructor (API 14) that supports passing the engine.
    // Choose the engine that supports the selected language, if there are several
    // then let the user choose.
    mTts = new TextToSpeech(context, listener);
    Log.i("Default TTS engine:" + mTts.getDefaultEngine());
}
 
Example 12
Source File: TtsProvider.java    From AlexaAndroid with GNU General Public License v2.0 5 votes vote down vote up
public TtsProvider(Context context, TextToSpeech.OnInitListener listener) {
    // TODO: use the 3-arg constructor (API 14) that supports passing the engine.
    // Choose the engine that supports the selected language, if there are several
    // then let the user choose.
    mTts = new TextToSpeech(context, listener);
    mAudioPauser = new AudioPauser(context, false);
    Log.i("Default TTS engine:" + mTts.getDefaultEngine());
}
 
Example 13
Source File: MainActivity.java    From reader with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	mTakePhoto = (Button) findViewById(R.id.take_photo);
	mImageView = (ImageView) findViewById(R.id.imageview);
	speak1 = (Button) findViewById(R.id.speak1);
	responseText = (TextView) findViewById(R.id.textView1);

	mTakePhoto.setOnClickListener(this);
	speak1.setOnClickListener(this);
	
	tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
           @Override
           public void onInit(int status) {
               // TODO Auto-generated method stub
               if (status == TextToSpeech.SUCCESS)
               {
                   int result = tts.setLanguage(Locale.CHINA);
                   if (result == TextToSpeech.LANG_MISSING_DATA
                           || result == TextToSpeech.LANG_NOT_SUPPORTED)
                   {
                       Toast.makeText(getApplicationContext(), "Language is not available.",
                               Toast.LENGTH_SHORT).show();
                   }
               } else {
               	Toast.makeText(getApplicationContext(), "Language is available.",
                           Toast.LENGTH_SHORT).show();
               }
           }
       });
}
 
Example 14
Source File: LiteracyApplication.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
@Override
    public void onCreate() {
        Log.i(getClass().getName(), "onCreate");
        super.onCreate();

        // Check if the application's versionCode was upgraded
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int oldVersionCode = sharedPreferences.getInt(PREF_APP_VERSION_CODE, 0);
        int newVersionCode = VersionHelper.getAppVersionCode(getApplicationContext());
        if (oldVersionCode == 0) {
            sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, newVersionCode).commit();
            oldVersionCode = newVersionCode;
        }
        if (oldVersionCode < newVersionCode) {
            Log.i(getClass().getName(), "Upgrading application from version " + oldVersionCode + " to " + newVersionCode);
//            if (oldVersionCode < ???) {
//                // Put relevant tasks required for upgrading here
//            }
            sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, newVersionCode).commit();
        }

        // Initialize TTS
        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                Log.i(getClass().getName(), "TextToSpeech onInit");
                Log.i(getClass().getName(), "TextToSpeech status: " + status);
            }
        });
    }
 
Example 15
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 16
Source File: TextToSpeechCompatUtils.java    From brailleback with Apache License 2.0 5 votes vote down vote up
/**
 * The constructor for the TextToSpeech class, using the given TTS engine.
 * This will also initialize the associated TextToSpeech engine if it isn't
 * already running.
 *
 * @param context The context this instance is running in.
 * @param listener The
 *            {@link android.speech.tts.TextToSpeech.OnInitListener} that
 *            will be called when the TextToSpeech engine has initialized.
 * @param engine Package name of the TTS engine to use.
 */
public static TextToSpeech newTextToSpeech(Context context,
        TextToSpeech.OnInitListener listener, String engine) {
    final TextToSpeech result = (TextToSpeech) CompatUtils.newInstance(CONSTRUCTOR_LLS,
            context, listener, engine);

    if (result != null) {
        return result;
    }

    return new TextToSpeech(context, listener);
}
 
Example 17
Source File: A2dpSinkActivity.java    From sample-bluetooth-audio with Apache License 2.0 5 votes vote down vote up
private void initTts() {
    mTtsEngine = new TextToSpeech(A2dpSinkActivity.this,
            new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status == TextToSpeech.SUCCESS) {
                        mTtsEngine.setLanguage(Locale.US);
                    } else {
                        Log.w(TAG, "Could not open TTS Engine (onInit status=" + status
                                + "). Ignoring text to speech");
                        mTtsEngine = null;
                    }
                }
            });
}
 
Example 18
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {
  if (requestCode == TTS_DATA_CHECK) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
      tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        public void onInit(int status) {
          if (status == TextToSpeech.SUCCESS) {
            ttsIsInit = true;
            if (tts.isLanguageAvailable(Locale.UK) >= 0)
              tts.setLanguage(Locale.UK);
            tts.setPitch(0.8f);
            tts.setSpeechRate(1.1f);
            speak();
          }
        }
      });
    } else {
      Intent installVoice = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
      startActivity(installVoice);
    }
  }

  // Listing 14-3: Finding the results of a speech recognition request
  if (requestCode == VOICE_RECOGNITION && resultCode == RESULT_OK) {
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    float[] confidence = data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);
    // TODO Do something with the recognized voice strings
  }
}
 
Example 19
Source File: MainActivity.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 5 votes vote down vote up
public void createTTS()
{
    t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {
            if(i==TextToSpeech.SUCCESS)
            {
                t1.setLanguage(Locale.ENGLISH);
            }
        }
    });
}
 
Example 20
Source File: MainService.java    From android-play-games-in-motion with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    // The service is being created.
    Utils.logDebug(TAG, "onCreate");
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_1);
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_2);
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_3);
    registerReceiver(mReceiver, intentFilter);

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // Determines the behavior for handling Audio Focus surrender.
    mAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS) {

                if (mTextToSpeech.isSpeaking()) {
                    mTextToSpeech.setOnUtteranceProgressListener(null);
                    mTextToSpeech.stop();
                }

                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }

                // Abandon Audio Focus, if it's requested elsewhere.
                mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);

                // Restart the current moment if AudioFocus was lost. Since AudioFocus is only
                // requested away from this application if this application was using it,
                // only Moments that play sound will restart in this way.
                if (mMission != null) {
                    mMission.restartMoment();
                }
            }
        }
    };

    // Asynchronously prepares the TextToSpeech.
    mTextToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                // Check if language is available.
                switch (mTextToSpeech.isLanguageAvailable(DEFAULT_TEXT_TO_SPEECH_LOCALE)) {
                    case TextToSpeech.LANG_AVAILABLE:
                    case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                    case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                        Utils.logDebug(TAG, "TTS locale supported.");
                        mTextToSpeech.setLanguage(DEFAULT_TEXT_TO_SPEECH_LOCALE);
                        mIsTextToSpeechReady = true;
                        break;
                    case TextToSpeech.LANG_MISSING_DATA:
                        Utils.logDebug(TAG, "TTS missing data, ask for install.");
                        Intent installIntent = new Intent();
                        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                        startActivity(installIntent);
                        break;
                    default:
                        Utils.logDebug(TAG, "TTS local not supported.");
                        break;
                }
            }
        }
    });

    mMediaPlayer = new MediaPlayer();
}