I have an HD Desire phone with android 2.3.
The TTS is working fine and it speaks every text I give. But when I use this either of the lines below to set my own voice for some texts, it simply ignores it and synthesizes the text, just like the line is not written!
tts.addSpeech("salam", "/sdcard/salam.wav");
tts.addSpeech("shalam", "com.company.appname", R.raw.shalam);
...
tts.speak("salam", TextToSpeech.QUEUE_FLUSH, null); //<--This isn't playing my voice file.
tts.speak("shalam", TextToSpeech.QUEUE_FLUSH, null); //<--Neither is this
I am sure of the existence of both files.
Why is that? Is there any restriction on the sound files? For example on their Frequency, or being mono or stereo?
I already checked the docs and saw nothing related.
OK, I found my problem, very silly situation which wasted several hours of mine!! I hope it will help if someone makes my mistake.
We should postpone this mapping of texts to the point TTS is successfully initialized, for example in onInit function:
#Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS)
{
tts.setLanguage(Locale.US);
mapVoices();
}
else
...
}
private void mapVoices()
{
tts.addSpeech("salam", "/sdcard/salam.wav");
tts.addSpeech("shalam", "com.company.appname", R.raw.shalam);
//...
}
Related
I am trying to implement Text-to-speech in my android auto app but unable to hear it on Desktop head unit. I have also tried it on Car Head unit but no luck.
below is the code i am using
TextToSpeech tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
and on button click i am using
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
also tried to turn OFF Bluetooth Headset as suggested in xda forums
but still same issue.
Do i need to add any extra library like "gear.aar" to make it work for Auto as the code is in CarActivity
speak() method requires four parameters to execute the job
I solved this using below code
tts.speak(readText, TextToSpeech.QUEUE_ADD, null, ""+Calendar.getInstance().getTimeInMillis());
I had the same problem.
This is what should be done:
be sure you call tts.speak(...) after TTS is initialized (return 0 - SUCCESS)
turn off bluetooth on the computer
I have an application that according to some events, changes a normal notification to text-to-speech in order to since sometimes the phone isn't available to users, and it'll be safer not to handle the phone.
For example, when you're driving, this is dangerous, so i want to turn the notifications to text-to-speech.
I've looked for a long time some explanation for turning text-to-speech when driving, but i can't find any reference for that no where i search.
For generating text-to-speech, i have this part, which works fine :
private TextToSpeech mTextToSpeech;
public void sayText(Context context, final String message) {
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
try {
if (mTextToSpeech != null && status == TextToSpeech.SUCCESS) {
mTextToSpeech.setLanguage(Locale.US);
mTextToSpeech.speak(message, TextToSpeech.QUEUE_ADD, null);
}
} catch (Exception ex) {
System.out.print("Error handling TextToSpeech GCM notification " + ex.getMessage());
}
}
});
}
But, i don't know how to check if i'm currently driving or not.
As Ashwin suggested, you can use Activity recognition Api, but there's a downside of that, the driving samples you'll receive, has a field of 'confidence' which isn't always accurate, so you'll have to do extra work(such as check locations to see if you actually moved) in order to fully know if the user moved.
You can use google's FenceApi which allows you to define a fence of actions such as driving, walking, running, etc. This api launched recently. If you want a sample for using it, you can use this answer.
You can pull this git project (everything free), which does exactly what you want : adds to the normal notification a text-to-speech when you're driving.
In order to know whether you are driving or not you can use Activity Recognition API
Here is a great tutorial that might help you out Tutorial and Source Code
I have referred speak-with-tts-such-as-hindi
and i have done like this:
extToSpeech text2speechHi;
text2speechHi=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
text2speechHi.setLanguage(new Locale("hin"));
}
}
});
text2speechHi.speak(getResources().getString(R.string.hindi_text), TextToSpeech.QUEUE_FLUSH, null);
But still not reading....
how to see the output ? whether change any device setting in which i run the code?? or need to install anything??
what is this eSpeak they have mention in that link...any other free option available?? Any one who implemented this successfully????
It is working in fine for English text2speech.setLanguage(Locale.UK);
You are calling speak() before initialization is done, i.e., before onInit is called, consequently setLanguage hasn't been called yet. Refactor your code to follow the asynchronous pattern, by e.g. moving speak() into the onInit method.
EDIT: OP says code snippet in question is not representative of the actual code. Awaiting clarification before removing or updating answer.
I just want to make a quiz-kind of application where questions are asked & shown.
I am using the Android's default Text to Speech engine, instead of mediaplayer(I have no choice except tts plz...) .I am initializing tts & using it in mainmethod("switchingLogic()").
Now, My problem is : The sound of first question should be played on start of the activity.But tts is not playing that sound if I place my SWITCHINGLOGIC() in onCreate().So, I have Placed it in the onInit() of tts , Desired output is coming, but "when the screen is locked & Unlocked, it is starting from the first question again."
Now, how can I make my app to start from there only if app goes background or phone is Locked automatically.
public void playQuestions(String sound) {
tts.speak(sound, TextToSpeech.QUEUE_FLUSH, null);
}
public void switchingLogic() {
playQuestion(item.getSounds(1));
--------------------
--------------------
}
#Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
if (arg0 == TextToSpeech.SUCCESS) {
Log.d("Yes", "SUCCESS");
switchingLogic();
}
}
Disastrous Situation Please help me, this is new issue & wanna submit the app in 10 hours...
This is happening because android supports only one thread in foreground, so when you lock the screen and unlock, your application goes to background and the present state of your application will be lost... as you have used
playQuestion(item.getSounds(1));
It makes ur application to open again and start from 1st Question.
I'm making use of Text to Speech - TTS in my Android app.. I've pasted the code below.. TTS is working fine, however the voice/text it speaks is not very clear.. It speaks really quick, so that it is not fully understandable.. I tried setting Locale.US, and used setPitch or setSpeechRate but it is not really convincing. I felt that there is some problem with my phone (Samsung S2).. So tried installing Google Translate TTS app from Google Play store.. In that app the voice was really clear.
My app will be used by Kids.. so want to make sure that the voice is really clear.
I'm breaking my head for the past few days to fix this problem.. Would be great if you could give me some pointers on where I'm doing wrong or how to improve??
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.getDefault());
//tts.setSpeechRate((float) 0.8);
//tts.setPitch(1.0f);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut(0);
}
} else {
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
private void speakOut(int position) {
tts.speak("Some text goes here", TextToSpeech.QUEUE_FLUSH, null);
}
Note: I tried setting value as 0.5f,0.8f etc., in both setPitch and setSpeechRate but still all the voice is not really clear as in GoogleTranslate App.
Your code looks fine.
All tts libraries are shared across the system. Samsung comes with its own tts library. Rest of phones use Pico TTS. The great thing is that your app is independent from the library and you can download as many TTS libs as you want so that when you request the TTS intent the user will be prompt a pop-up to select which of their TTS synth they want for your app.
For me Pico TTS was working fine. Velocity / speech rate was normal, I just put the tone (pitch) a bit up to de-robotize the feeling a bit.
tts.setPitch(1.1f);
Try with Pico TTS and answer back.