I am confused with a code which will speaks out the word from an edit text,The code i used is not speaking out but it getting the value,no of characters etc correctly.
The code i used is..
public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("Entered to the Activity");
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
speakOut();
}
});
}
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
System.out.println("Entered to OnDestroy");
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
System.out.println("Enterd init Function");
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
System.out.println("Entered Speakout");
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
It returning a message that languagge not supported..How can i rectify it
Try this it might help you.
I think in your device speech synthesizer is not installed so use these for this. If not installed it will redirect to google play to install this.
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = talker.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
Try out below code. It works fine for me i hope it will help you also.
public class TexttoSpeechActivity extends Activity implements OnClickListener,
OnInitListener {
private Button m_btnSpech;
private EditText m_etText;
// TTS object
private TextToSpeech m_tts;
// status check code
private final int m_data = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_btnSpech = (Button) findViewById(R.id.mbtnSpeak);
m_etText = (EditText) findViewById(R.id.metTextValues);
// listen for clicks
m_btnSpech.setOnClickListener(this);
// check for TTS data intent to check if a TTS engine is installed
Intent m_intent = new Intent();
m_intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(m_intent, m_data);
}
// respond to button click.
#Override
public void onClick(View p_v) {
// get the text entered.
String m_word = m_etText.getText().toString();
speakWord(m_word);
}
/**
* Executed when a new TTS is instantiated. Some text is spoken via TTS
* here.
*
* #param p_word
* -contains the word entered into the edittext.
*/
private void speakWord(String p_word) {
// speak straight away
m_tts.speak(p_word, TextToSpeech.QUEUE_FLUSH, null);
}
/**
* This is the callback from the TTS engine check, if a TTS is installed we
* create a new TTS instance (which in turn calls onInit), if not then we
* will create an intent to go off and install a TTS engine
*
* #param p_requestCode
* int Request code returned from the check for TTS engine.
* #param p_resultCode
* int Result code returned from the check for TTS engine.
* #param p_data
* Intent Intent returned from the TTS check.
*/
#Override
protected void onActivityResult(int p_requestcode, int p_resultcode,
Intent p_data) {
if (p_requestcode == m_data) {
if (p_resultcode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// the user has the necessary data - create the TTS
m_tts = new TextToSpeech(this, this);
} else {
// no data - install it now
Intent m_intnt = new Intent();
m_intnt.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(m_intnt);
}
}
}
// setup TTS
#Override
public void onInit(int p_status) {
// check for successful instantiation
if (p_status == TextToSpeech.SUCCESS) {
if (m_tts.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
m_tts.setLanguage(Locale.US);
Toast.makeText(this, "Text To Speech ", Toast.LENGTH_LONG)
.show();
}
Toast.makeText(TexttoSpeechActivity.this,
"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
} else if (p_status == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...",
Toast.LENGTH_LONG).show();
}
}
/**
* Be kind, once you've finished with the TTS engine, shut it down so other
* applications can use it without us interfering with it.
*/
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (m_tts != null) {
m_tts.stop();
m_tts.shutdown();
}
super.onDestroy();
}
}
Related
I am using PocketSphinx for android-23.
I want to code an offline assistant for one of my apps.
I have successfully used recognizer.addKeyphraseSearch to initialize the assistant. For eg. In this case I say "Hello" to initialize it.
this is my entire code
public class Farmax_2 extends Activity implements
RecognitionListener {
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
private static final String ahead = "about";
private static final String PHONE_SEARCH = "ahead";
private static final String MENU_SEARCH = "menu";
TextToSpeech t1;
Button btn;
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "hello";
/* Used to handle permission request */
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
private SpeechRecognizer recognizer;
private HashMap<String, Integer> captions;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_farmax_2);
// Check if user has given permission to record audio
int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
return;
}
btn=(Button)findViewById(R.id.buttonme);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent inte = new Intent(Farmax_2.this, MainMenu.class);
startActivity(inte);
}
});
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
runRecognizerSetup();
}
public void omku(View view) { Intent in=new Intent(this,abtus.class);
startActivity(in);}
private void runRecognizerSetup() {
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
#Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(Farmax_2.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
#Override
protected void onPostExecute(Exception result) {
if (result != null) {
} else {
switchSearch(KWS_SEARCH);
}
}
}.execute();
}
#Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
} else {
finish();
}
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
}
}
/**
* In partial result we get quick updates about current hypothesis. In
* keyword spotting mode we can react here, in other modes we need to wait
* for final result in onResult.
*/
#Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
switch (text) {
case KEYPHRASE: {
omkar();
break;
}
case ahead: {
Intent in = new Intent(this, abtus.class);
startActivity(in);
break;
// t1.speak("taking you to privacy policy of farmax.", TextToSpeech.QUEUE_FLUSH, null);
}
case PHONE_SEARCH: {
Intent in = new Intent(this, MainMenu.class);
startActivity(in);
// t1.speak("Main Menu.", TextToSpeech.QUEUE_FLUSH, null);
break;
}
}
}
private void omkar() {
t1.speak("Yes sir.", TextToSpeech.QUEUE_FLUSH, null);
switchSearch(MENU_SEARCH);
}
/**
* This callback is called when we stop the recognizer.
*/
#Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBeginningOfSpeech() {
}
/**
* We stop recognizer here to get a final result
*/
#Override
public void onEndOfSpeech() {
}
private void switchSearch(String searchName) {
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
if (searchName.equals(KWS_SEARCH))
recognizer.startListening(searchName);
else
recognizer.startListening(searchName, 10000);
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.getRecognizer();
recognizer.addListener(this);
/** In your application you might not need to add all those searches.
* They are added here for demonstration. You can leave just one.
*/
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, "hello");
// Create grammar-based search for selection between demos
File menuGrammar = new File(assetsDir, "firstscn.gram");
recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
// Create grammar-based search for digit recognition
}
#Override
public void onError(Exception error) {
}
#Override
public void onTimeout() {
switchSearch(KWS_SEARCH);
}
}
when I say hello, it responds correctly by replying "Yes sir" via tts. But after that it is supposed to switch menu and wait for the further commands. In this case there are two.
#Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
switch (text) {
case KEYPHRASE: {
omkar();
break;
}
case ahead: {
Intent in = new Intent(this, abtus.class);
startActivity(in);
break;
// t1.speak("taking you to privacy policy of farmax.", TextToSpeech.QUEUE_FLUSH, null);
}
case PHONE_SEARCH: {
Intent in = new Intent(this, MainMenu.class);
startActivity(in);
// t1.speak("Main Menu.", TextToSpeech.QUEUE_FLUSH, null);
break;
}
}
}
But the problem is that It doesnt wait for my command after it switches the menu.
Sometimes a toast pops up with "about" or sometimes with "ahead" even though I dont speak them. The app freezes badly after that and leaves me no other option than to close it.
If have also tried if else statements other than switch and case. But they don't seem to help much.
I have also tried to use the above code in onResult rather than onPartialResult but that doesnt help as well.
using sphinx tools I have created my own dictionary and grammar file.
Here is the grammar file content for this case.
#JSGF V1.0;
grammar firstscn;
public <item> = about | ahead;
Where am I going wrong? Please help me.
When I check which languages are available Thai (th)is available butit doesn't read the text
#SuppressLint("NewApi")
private void speak() {
if(tts!=null && tts.isSpeaking()){
tts.stop();
}else{
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.forLanguageTag("th")); //tts.getAvailableLanguages().;
tts.setSpeechRate(0.7f);
}
}
#Override
public void onInit(int status) {
tts.speak("ซึ่งมีระยะทางส่วนใหญ่เป็น ทางหลวงแผ่นดินหมายเลข (สายบางนา - หาดเล็ก) เป็นเส้นทางคมนาคมหลักเส้นหนึ่งของประเทศไทย ", TextToSpeech.QUEUE_FLUSH, null);
}
Edit your code like this:
#SuppressLint("NewApi")
private void speak() {
if(tts!=null && tts.isSpeaking()) {
tts.stop();
}else{
tts = new TextToSpeech(this, this);
}
}
#Override public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int res = tts.setLanguage("th_TH");
//tts.getAvailableLanguages().;
tts.setSpeechRate(0.7f);
if (res >= TextToSpeech.LANG_AVAILABLE) {
tts.speak("ซึ่งมีระยะทางส่วนใหญ่เป็น ทางหลวงแผ่นดินหมายเลข (สายบางนา - หาดเล็ก) เป็นเส้นทางคมนาคมหลักเส้นหนึ่งของประเทศไทย ", TextToSpeech.QUEUE_FLUSH, null);
}
}
Because TextToSpeech instance is created asynchronously, so you can hear synthesis result when you control your tts after onInit() method was done.
I am trying to make an application that uses Speech to text, and Text to speech.
So the algorithm of this program is:
1 - when the user runs this program, it will call voice Recognition
2 - after getting the input from the user, the program will repeat the same word the user said.
here's my code:
public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener {
protected static final int REQUEST_OK = 1234;
String userSay;
TextView text1;
private TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
findViewById(R.id.button1).setOnClickListener(this);
text1 = (TextView) findViewById(R.id.text1);
tts=new TextToSpeech(this, this);
}
#Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onPause(){
if(tts !=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Log.e("TTS", "Initilization Success!");
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
String anyThing = " ";
speakIt(anyThing);
}
return;
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakIt(String someThing) {
Log.e("something: ", someThing);
tts.speak(someThing, TextToSpeech.QUEUE_ADD, null);
Log.e("TTS", "called");
}
#Override
public void onClick(View v) {
//Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
text1.setText(" ");
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "I'm listen to you...");
try {
startActivityForResult(i, REQUEST_OK);
} catch (Exception e) {
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQUEST_OK && resultCode==-1) {
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
userSay = thingsYouSaid.get(0);
}
try {
String FinalValue = getDataMethod(hasil);
text1.setText(FinalValue);
Log.v("Status OK: ", FinalValue);
speakIt(FinalValue);
Log.v("speakIt: ", "called");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
private String getDataMethod(String num) throws IllegalStateException, IOException {
num = "You say, " + num;
return num;
}
}
But the application won't say anything. And in the logcat I got an error that says:
TextToSpeech(10335): speak failed: not bound to TTS
When before I made this application, I made it in separate parts: Text to Speech, and Speech to Text.
Both of that applications ran normally.
I don't know why this error occurs.
Can anyone help me?
One problem is that you are calling:
speakIt(FinalValue);
inside:
onActivityResult(
while in onPause, you execute:
if(tts !=null){
tts.stop();
tts.shutdown();
}
so once, you open your sub activity, onPause is called and tts is shutdown, returning back you use such destroyed tts which is wrong and can cause such behaviour.
You can move your code from onPause to onStop, and your init code to onStart. In onActivityResult set some class instance variable with data to speak inside onInit.
I am using the following code. Its working fine except a problem that i have list of text to converted as speech. But its only converting last line as a speech. Here is my code where I am putting data in listview and trying to convert it into speech:
public class TextSpeech extends ListActivity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private TextView txtText;
private List<Message> mess;
List<String> titless;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speach);
tts = new TextToSpeech(this, this);
BaseFeedParser parser = new BaseFeedParser();
mess = parser.parse();
titless = new ArrayList<String>(mess.size());
speakOut();
}
#SuppressLint("NewApi")
#Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#SuppressLint("NewApi")
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakOut() {
for (Message msg : mess){
titless.add(msg.getTitle());
tts.speak(msg.getTitle(), TextToSpeech.QUEUE_FLUSH, null);
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titless);
this.setListAdapter(adapter);
}
}
You had used TextToSpeech.QUEUE_FLUSH just changed it to TextToSpeech.QUEUE_ADD .According to your requirement you want TTS to read one by one.
Does anybody have idea why the TextToSpeech method I have here doesn't work anymore on Android 4.1?
On the sayIt() method it always returns Log.i(TAG, "Failure: TextToSpeech instance tts was not properly initialized");
public class SpeakSuperActivity extends Activity implements OnInitListener {
private final static String TAG = "TextToSpeech";
protected static final Locale defaultLocale = Locale.GERMAN;
private static int TTS_DATA_CHECK = 100;
protected TextToSpeech tts = null;
protected boolean ttsIsInit = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use an Intent and startActivityForResult to check whether TTS data installed on the
// device. Result returned and acted on in method onActivityResult(int, int, Intent) below.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, TTS_DATA_CHECK);
}
// protected void initTextToSpeech() {
// Log.d(TAG, "TTS initTextToSpeech() called");
// Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
// startActivityForResult(intent, TTS_DATA_CHECK);
// }
protected void onActivityResultOld(int requestCode, int resultCode, Intent data) {
if (requestCode == TTS_DATA_CHECK) {
Log.d(TAG, "onActivityResult: TTS_DATA_CHECK fork reached");
if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
Log.d(TAG, "Data installed: Engine.CHECK_VOICE_DATA_PASS fork reached");
tts = new TextToSpeech(this, this);
// tts = new TextToSpeech(this, new OnInitListener() {
// public void onInit(int status) {
// if (status == TextToSpeech.SUCCESS) {
// Log.w(TAG, "TTS onInit() success :)");
// ttsIsInit = true;
// if (tts.isLanguageAvailable(Locale.UK) >= 0) {
// Log.d(TAG, "Local UK available");
// tts.setLanguage(Locale.UK);
// }
// if (tts.isLanguageAvailable(Locale.GERMAN) >= 0) {
// Log.d(TAG, "Local German available");
// tts.setLanguage(Locale.GERMAN);
// }
// tts.setPitch(0.8f);
// tts.setSpeechRate(1.1f);
// speak("Hallo Sprücheklopfer");
// } else if (status == TextToSpeech.ERROR) {
// Log.w(TAG, "TTS onInit() failed :(");
// }
// }
// });
} else {
Log.i(TAG, "TTS not installed yet, calling intent to install...");
Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installVoice);
}
}
}
// Create the TTS instance if TextToSpeech language data are installed on device. If not
// installed, attempt to install it on the device.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TTS_DATA_CHECK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// Success, so create the TTS instance. But can't use it to speak until
// the onInit(status) callback defined below runs, indicating initialization.
Log.i(TAG, "Success, let's talk");
// XXX: NOTE: it is REALLY important to use new TextToSpeech(getApplicationContext(), this) instead of new TextToSpeech(this, this)!
tts = new TextToSpeech(getApplicationContext(), this);
// // Use static Locales method to list available locales on device
// Locale locales [] = Locale.getAvailableLocales();
// Log.i(TAG,"Locales Available on Device:");
// for(int i=0; i<locales.length; i++){
// String temp = "Locale "+i+": "+locales[i]+" Language="
// +locales[i].getDisplayLanguage();
// if(locales[i].getDisplayCountry() != "") {
// temp += " Country="+locales[i].getDisplayCountry();
// }
// Log.i(TAG, temp);
// }
} else {
// missing data, so install it on the device
Log.i(TAG, "Missing Data; Install it");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
// Set to a language locale after checking availability
Log.i(TAG, "defaultLocaleAvailable=" + tts.isLanguageAvailable(defaultLocale));
tts.setLanguage(defaultLocale);
// Examples of voice controls. Set to defaults of 1.0.
tts.setPitch(1.0F);
tts.setSpeechRate(1.0F);
// Issue a greeting and instructions in the default language
// speakGreeting(defaultLocale.getDisplayLanguage());
// sayIt("Hallo Sprücheklopfer", true);
} else {
ttsIsInit = false;
Log.i(TAG, "Failure: TextToSpeech instance tts was not properly initialized");
}
}
// protected void speak(String text) {
// Log.d(TAG, "TTS sould say: " + text);
// if (tts != null && ttsIsInit) {
// tts.speak(text, TextToSpeech.QUEUE_ADD, null);
// } else {
// Log.w(TAG, "TTS not initialised or null");
// }
// }
// Method to speak a string. The boolean flushQ determines whether the text is
// appended to the queue (if false), or if the queue is flushed first (if true).
public void sayIt(String text, boolean flushQ) {
if (tts != null && ttsIsInit) {
if (flushQ) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
} else {
Log.i(TAG, "Failure: TextToSpeech instance tts was not properly initialized");
}
}
// Release TTS resources when finished
#Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
ttsIsInit = false;
}
super.onDestroy();
}
public void onInitOld(int status) {
Log.i(TAG, "TTS onInit() status :" + status);
if (status == TextToSpeech.SUCCESS) {
Log.w(TAG, "TTS onInit() success :)");
ttsIsInit = true;
if (tts.isLanguageAvailable(Locale.UK) >= 0) {
Log.d(TAG, "Local UK available");
tts.setLanguage(Locale.UK);
}
if (tts.isLanguageAvailable(Locale.GERMAN) >= 0) {
Log.d(TAG, "Local German available");
tts.setLanguage(Locale.GERMAN);
}
tts.setPitch(0.8f);
tts.setSpeechRate(1.1f);
sayIt("Hallo Sprücheklopfer", true);
} else if (status == TextToSpeech.ERROR) {
Log.w(TAG, "TTS onInit() failed :(");
} else {
Log.w(TAG, "TTS onInit() unknown status :" + status);
}
}
}