Search in sources :

Example 56 with TextToSpeech

use of android.speech.tts.TextToSpeech in project platform_frameworks_base by android.

the class TextToSpeechTests method blockingInitAndVerify.

private void blockingInitAndVerify(final String engine, int errorCode) throws InterruptedException {
    TextToSpeech.OnInitListener listener = Mockito.mock(TextToSpeech.OnInitListener.class);
    final CountDownLatch latch = new CountDownLatch(1);
    doCountDown(latch).when(listener).onInit(errorCode);
    mTts = new TextToSpeech(getInstrumentation().getTargetContext(), listener, engine, MOCK_PACKAGE, false);
    awaitCountDown(latch, 5, TimeUnit.SECONDS);
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 57 with TextToSpeech

use of android.speech.tts.TextToSpeech in project LEGO-MINDSTORMS-MINDdroid by NXT.

the class MINDdroid method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address and start a new bt communicator thread
                String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                pairing = data.getExtras().getBoolean(DeviceListActivity.PAIRING);
                startBTCommunicator(address);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            switch(resultCode) {
                case Activity.RESULT_OK:
                    btOnByUs = true;
                    selectNXT();
                    break;
                case Activity.RESULT_CANCELED:
                    showToast(R.string.bt_needs_to_be_enabled, Toast.LENGTH_SHORT);
                    finish();
                    break;
                default:
                    showToast(R.string.problem_at_connecting, Toast.LENGTH_SHORT);
                    finish();
                    break;
            }
            break;
        // will not be called now, since the check intent is not generated                
        case TTS_CHECK_CODE:
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                mTts = new TextToSpeech(this, this);
            } else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
            break;
    }
}
Also used : Intent(android.content.Intent) TextToSpeech(android.speech.tts.TextToSpeech)

Example 58 with TextToSpeech

use of android.speech.tts.TextToSpeech in project LEGO-MINDSTORMS-MINDdroid by NXT.

the class MINDdroid method onCreate.

/**
     * Called when the activity is first created. Inititializes all the
     * graphical views.
     */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    thisActivity = this;
    mRobotType = this.getIntent().getIntExtra(SplashMenu.MINDDROID_ROBOT_TYPE, R.id.robot_type_shooterbot);
    setUpByType();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    StartSound mySound = new StartSound(this);
    mySound.start();
    // setup our view, give it focus and display.
    mView = new GameView(getApplicationContext(), this);
    mView.setFocusable(true);
    setContentView(mView);
    reusableToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    // experimental TTS support for the lejosMINDdroid project
    mTts = new TextToSpeech(this, // TextToSpeech.OnInitListener
    this);
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech)

Example 59 with TextToSpeech

use of android.speech.tts.TextToSpeech in project Osmand by osmandapp.

the class TTSCommandPlayerImpl method initializeEngine.

private void initializeEngine(final Context ctx, final Activity act) {
    if (mTtsContext != ctx) {
        internalClear();
    }
    if (mTts == null) {
        mTtsContext = ctx;
        ttsVoiceStatus = "";
        ttsVoiceUsed = "";
        ttsRequests = 0;
        final float speechRate = cSpeechRate;
        final String[] lsplit = (language + "____.").split("[\\_\\-]");
        // constructor supports lang_country_variant
        Locale newLocale0 = new Locale(lsplit[0], lsplit[1], lsplit[2]);
        // #3344: Try Locale builder instead of constructor (only available from API 21). Also supports script (for now supported as trailing x_x_x_Scrp)
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            try {
                newLocale0 = new Locale.Builder().setLanguage(lsplit[0]).setScript(lsplit[3]).setRegion(lsplit[1]).setVariant(lsplit[2]).build();
            } catch (RuntimeException e) {
            // Falls back to constructor
            }
        }
        final Locale newLocale = newLocale0;
        mTts = new TextToSpeech(ctx, new OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.SUCCESS) {
                    ttsVoiceStatus = "NO INIT SUCCESS";
                    internalClear();
                } else if (mTts != null) {
                    speechAllowed = true;
                    switch(mTts.isLanguageAvailable(newLocale)) {
                        case TextToSpeech.LANG_MISSING_DATA:
                            if (isSettingsActivity(act)) {
                                AlertDialog.Builder builder = createAlertDialog(R.string.tts_missing_language_data_title, R.string.tts_missing_language_data, new IntentStarter(act, TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA), act);
                                builder.show();
                            }
                            ttsVoiceStatus = newLocale.getDisplayName() + ": LANG_MISSING_DATA";
                            ttsVoiceUsed = getVoiceUsed();
                            break;
                        case TextToSpeech.LANG_AVAILABLE:
                            ttsVoiceStatus = newLocale.getDisplayName() + ": LANG_AVAILABLE";
                        case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                            ttsVoiceStatus = "".equals(ttsVoiceStatus) ? newLocale.getDisplayName() + ": LANG_COUNTRY_AVAILABLE" : ttsVoiceStatus;
                        case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                            try {
                                mTts.setLanguage(newLocale);
                            } catch (Exception e) {
                                e.printStackTrace();
                                mTts.setLanguage(Locale.getDefault());
                            }
                            if (speechRate != 1) {
                                mTts.setSpeechRate(speechRate);
                            }
                            ttsVoiceStatus = "".equals(ttsVoiceStatus) ? newLocale.getDisplayName() + ": LANG_COUNTRY_VAR_AVAILABLE" : ttsVoiceStatus;
                            ttsVoiceUsed = getVoiceUsed();
                            break;
                        case TextToSpeech.LANG_NOT_SUPPORTED:
                            // maybe weird, but I didn't want to introduce parameter in around 5 methods just to do this if condition
                            if (isSettingsActivity(act)) {
                                AlertDialog.Builder builder = createAlertDialog(R.string.tts_language_not_supported_title, R.string.tts_language_not_supported, new IntentStarter(act, Intent.ACTION_VIEW, Uri.parse("market://search?q=text to speech engine")), act);
                                builder.show();
                            }
                            ttsVoiceStatus = newLocale.getDisplayName() + ": LANG_NOT_SUPPORTED";
                            ttsVoiceUsed = getVoiceUsed();
                            break;
                    }
                }
            }

            private boolean isSettingsActivity(final Context ctx) {
                return ctx instanceof SettingsActivity;
            }

            private String getVoiceUsed() {
                try {
                    if (android.os.Build.VERSION.SDK_INT >= 21) {
                        if (mTts.getVoice() != null) {
                            return mTts.getVoice().toString() + " (API " + android.os.Build.VERSION.SDK_INT + ")";
                        }
                    } else {
                        return mTts.getLanguage() + " (API " + android.os.Build.VERSION.SDK_INT + " only reports language)";
                    }
                } catch (RuntimeException e) {
                // mTts.getVoice() might throw NPE
                }
                return "-";
            }
        });
        mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

            // The call back is on a binder thread.
            @Override
            public synchronized void onUtteranceCompleted(String utteranceId) {
                if (--ttsRequests <= 0)
                    abandonAudioFocus();
                log.debug("ttsRequests=" + ttsRequests);
                if (ttsRequests < 0) {
                    ttsRequests = 0;
                }
            }
        });
    }
}
Also used : Locale(java.util.Locale) OnInitListener(android.speech.tts.TextToSpeech.OnInitListener) Context(android.content.Context) TextToSpeech(android.speech.tts.TextToSpeech) OnUtteranceCompletedListener(android.speech.tts.TextToSpeech.OnUtteranceCompletedListener) SettingsActivity(net.osmand.plus.activities.SettingsActivity)

Example 60 with TextToSpeech

use of android.speech.tts.TextToSpeech in project android_packages_apps_Settings by DirtyUnicorns.

the class TextToSpeechSettings method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.tts_settings);
    getActivity().setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
    mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
    mLocalePreference = (ListPreference) findPreference(KEY_ENGINE_LOCALE);
    mLocalePreference.setOnPreferenceChangeListener(this);
    mDefaultPitchPref = (SeekBarPreference) findPreference(KEY_DEFAULT_PITCH);
    mDefaultRatePref = (SeekBarPreference) findPreference(KEY_DEFAULT_RATE);
    mActionButtons = (LayoutPreference) findPreference(KEY_ACTION_BUTTONS);
    mPlayButton = (Button) mActionButtons.findViewById(R.id.tts_play_button);
    mPlayButton.setOnClickListener(this);
    mPlayButton.setEnabled(false);
    mResetButton = (Button) mActionButtons.findViewById(R.id.tts_reset_button);
    mResetButton.setOnClickListener(this);
    if (savedInstanceState == null) {
        mLocalePreference.setEnabled(false);
        mLocalePreference.setEntries(new CharSequence[0]);
        mLocalePreference.setEntryValues(new CharSequence[0]);
    } else {
        // Repopulate mLocalePreference with saved state. Will be updated later with
        // up-to-date values when checkTtsData() calls back with results.
        final CharSequence[] entries = savedInstanceState.getCharSequenceArray(STATE_KEY_LOCALE_ENTRIES);
        final CharSequence[] entryValues = savedInstanceState.getCharSequenceArray(STATE_KEY_LOCALE_ENTRY_VALUES);
        final CharSequence value = savedInstanceState.getCharSequence(STATE_KEY_LOCALE_VALUE);
        mLocalePreference.setEntries(entries);
        mLocalePreference.setEntryValues(entryValues);
        mLocalePreference.setValue(value != null ? value.toString() : null);
        mLocalePreference.setEnabled(entries.length > 0);
    }
    mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener);
    setTtsUtteranceProgressListener();
    initSettings();
    // Prevent restarting the TTS connection on rotation
    setRetainInstance(true);
}
Also used : TtsEngines(android.speech.tts.TtsEngines) TextToSpeech(android.speech.tts.TextToSpeech)

Aggregations

TextToSpeech (android.speech.tts.TextToSpeech)103 Test (org.junit.Test)34 Locale (java.util.Locale)18 TtsEngines (android.speech.tts.TtsEngines)15 ActivityNotFoundException (android.content.ActivityNotFoundException)12 Intent (android.content.Intent)12 ContentResolver (android.content.ContentResolver)10 MissingResourceException (java.util.MissingResourceException)10 IntentFilter (android.content.IntentFilter)8 TextView (android.widget.TextView)7 Config (org.robolectric.annotation.Config)7 BluetoothAdapter (android.bluetooth.BluetoothAdapter)6 Handler (android.os.Handler)6 UtteranceProgressListener (android.speech.tts.UtteranceProgressListener)6 ArrayAdapter (android.widget.ArrayAdapter)6 Context (android.content.Context)5 HashMap (java.util.HashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Bundle (android.os.Bundle)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3