Jan 4, 2013

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

Last two article demonstrate how to Get installed TTS engines and Get features supported by various installed TextToSpeech Engine. Now we can make it speak with the selected TTS Engine.

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


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.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnInitListener{
 
 TextView tvDefaultTTS, tvTTSInfo;
 EditText textIn;
 Button buttoSpeak;
 
 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);
        
        textIn = (EditText)findViewById(R.id.textin);
        textIn.setText("Android Coding"); //pre-set text
     buttoSpeak = (Button)findViewById(R.id.speak);
     buttoSpeak.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    String textToBeSpeech = textIn.getText().toString();
       tts.speak(textToBeSpeech, TextToSpeech.QUEUE_ADD, null);
    
   }});
     
 }
 
 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"/>
    <EditText
        android:id="@+id/textin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/speak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Speak"/>
    <TextView
        android:id="@+id/ttsinfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>


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.


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

Dec 27, 2012

Get device's Android ID

Secure.ANDROID_ID is a 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)

device's Android ID


package com.example.androidid;

import android.os.Bundle;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.widget.Toast;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  String id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
  Toast.makeText(getApplicationContext(), id, Toast.LENGTH_LONG).show();
 }

}


Dec 13, 2012

Read raw text file, and display in ScrollView.

This article demonstrate how to read raw text file from /res/raw/ folder, and display the text in a TextView inside ScrollView.

Read raw text file, and display in ScrollView.


Create raw text file at /res/raw/mytext.txt.
Santa Claus Is Coming To Town:

You better watch out
You better not cry
Better not pout
I'm telling you why
Santa Claus is coming to town

He's making a list,
And checking it twice;
Gonna find out Who's naughty and nice.
Santa Claus is coming to town

He sees you when you're sleeping
He knows when you're awake
He knows if you've been bad or good
So be good for goodness sake!

O! You better watch out!
You better not cry.
Better not pout, I'm telling you why.
Santa Claus is coming to town.
Santa Claus is coming to town.


Add a ScrollView with TextView inside, to our layout.
<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"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/mytextview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </ScrollView>
    
</LinearLayout>


Main code:
package com.AndroidTextResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView myTextView = (TextView)findViewById(R.id.mytextview);
  
  InputStream inputStream = getResources().openRawResource(R.raw.mytext);
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  
  String myText = "";
  int in;
  try {
   in = inputStream.read();
   while (in != -1)
   {
    byteArrayOutputStream.write(in);
    in = inputStream.read(); 
   }
   inputStream.close();
   
   myText = byteArrayOutputStream.toString(); 
  }catch (IOException e) {
   e.printStackTrace(); 
  }

  myTextView.setText(myText); 
 }
 
}



Infolinks In Text Ads