Dec 28, 2012

Get installed TTS engines, by calling getEngines() method.

Start from Android API level 14, List<TextToSpeech.EngineInfo> getEngines() method was provided to get a list of all installed TTS engines.

To display installed TTS Engines:

display installed TTS Engines


package com.example.androidtexttospeech;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnInitListener{
 
 TextView tvDefaultTTS;
 
 Spinner spInstalledEngines;
 TextToSpeech tts;
 List<TextToSpeech.EngineInfo> listInstalledEngines;
 List<String> listInstalledEnginesName;
 String defaultTTS;
 
 private ArrayAdapter<String> adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tvDefaultTTS = (TextView)findViewById(R.id.defaulttts);
  spInstalledEngines = (Spinner)findViewById(R.id.installedengines);

  tts = new TextToSpeech(this, this);
  listInstalledEngines = tts.getEngines();
  listInstalledEnginesName = new ArrayList<String>();

        for(int i = 0; i < listInstalledEngines.size(); i++){
         listInstalledEnginesName.add(listInstalledEngines.get(i).label);
        }
        
        adapter = new ArrayAdapter<String>(
    this, android.R.layout.simple_spinner_item, listInstalledEnginesName);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spInstalledEngines.setAdapter(adapter);
  
        defaultTTS = tts.getDefaultEngine();
        tvDefaultTTS.setText("Default TTS Engine: " + defaultTTS);
 }

 @Override
 public void onInit(int status) {
  // TODO Auto-generated method stub
  
 }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  tts.shutdown();
 }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/defaulttts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Spinner 
        android:id="@+id/installedengines"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


Next:
- Get features supported by TextToSpeech Engine

1 comment:

Infolinks In Text Ads