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.
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>
No comments:
Post a Comment