Jan 3, 2013

Get features supported by TextToSpeech Engine

From Android API level 15, getFeatures(Locale locale) method of TextToSpeech was provided to queries the engine for the set of features it supports for a given locale.

Here is a example demonstrate how to get supported features from TextToSpeech Engineer.

Get features supported by TextToSpeech Engine


package com.example.androidtexttospeech;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.EngineInfo;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
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, tvTTSInfo;
 
 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);
  tvTTSInfo = (TextView)findViewById(R.id.ttsinfo);
  
  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);
        
        spInstalledEngines.setOnItemSelectedListener(myOnItemSelectedListener);
 }
 
 OnItemSelectedListener myOnItemSelectedListener
 = new OnItemSelectedListener(){

  @Override
  public void onItemSelected(AdapterView<?> parent, View iew, int position,
    long id) {
   
   //shutdown old tts
   if(tts != null){
    tts.shutdown();
   }
   
   //switch to new tts
   EngineInfo selectedEngineInfo = listInstalledEngines.get(position);
   String engine = selectedEngineInfo.name;
   tts = new TextToSpeech(MainActivity.this, MainActivity.this, engine);
   
  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub
   
  }};
 
 private void updateTTSInfo(){
  
  String info = "";
  
  Locale locale = tts.getLanguage();
  
  if(locale != null){
   info += "Country: " + locale.getDisplayCountry() + "\n"
     + "Language: " + locale.getDisplayLanguage() + "\n";
   
   Set<String> feature = tts.getFeatures(locale);
   
   String[] featureArray = new String[feature.size()];
   feature.toArray(featureArray);
   for(int i = 0; i < featureArray.length; i++){
    info += "feature : " + featureArray[i] + "\n";
   }
  }
  
  tvTTSInfo.setText(info);
 }

 @Override
 public void onInit(int status) {
  updateTTSInfo();
 }

 @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"/>
    <TextView
        android:id="@+id/ttsinfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>


Please notice that in order to call getFeatures(Locale locale), android:minSdkVersion in AndroidManifest.xml have to be set at least "15".

Next: Implement TextToSpeech (TTS) function, with TTS engine selectable.


No comments:

Post a Comment

Infolinks In Text Ads