I am new at android programming, currently doing the speech application that req
ID: 3691714 • Letter: I
Question
I am new at android programming, currently doing the speech application that require the voice output from application.
Is there any good suggestion or free API I can use to develope, because I have search some of voice generator which most of request online service to fetch the data.
I would like to make offline(locally) speech device that don't need the wifi service.
Example: There are multiple button that content text or concept, when user click or selected one of button, the application will generate the voice whichever user selected.
So if user click "Hello" button ------> the application will generate the voice output -------> Hello sounds from the app.
Explanation / Answer
Try this code for speech
This is text to speech
U can add the buttons and use this code
1: Start or Open an Android Project
If you already have an application you want to implement Text To Speech with, open it in your IDE. Otherwise, create a new Android project. You can use the code in this tutorial with any Activity class. For demonstration, we will first create some user interface elements. Again, if you already have your own UI, you can use it instead.
2: Create User Interface Elements
Add some user interface elements to your application, allowing the user to enter text and initiate speech playback using a button. In the XML layout file for your Activity, which will be "main.xml" if you created a new project, add the following markup:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
<TextView android:id="@+id/intro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter some text:"
/>
<EditText android:id="@+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
/>
3: Listen For User Events
1
2
3
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
Alter the class declaration to implement the "OnClickListener" interface, as in the following sample line of code:
1
public class SpeakingAndroid extends Activity implements OnClickListener
Alter the class name to suit your own application details. Your IDE may display warnings because your class has not yet implemented "OnClickListener" correctly - just ignore these for now. In the "onCreate" method, add the following code:
1
2
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
If you did not add the button using "speak" as its ID in your XML layout file, alter this code to reflect the correct ID value. This sets your Activity class up to handle user button clicks. Add the following method outline to your class:
1
2
3
public void onClick(View v) {
//handle user clicks here
}
Inside this method you will begin the Text To Speech functionality.
Step 4: Get the Entered Text
When the user clicks the button, your app needs to get any text entered so that you can pass it to the TTS method. Add the following import statement at the top of your class declaration so that your code can refer to the editable text-field:
1
import android.widget.EditText;
Inside your "onClick" method, add the following code:
1
2
EditText enteredText = (EditText)findViewById(R.id.enter);
String words = enteredText.getText().toString();
Step 5: Create a Speech Method
1
2
3
private void speakWords(String speech) {
//implement TTS here
}
This is where your TTS processing will go. Back in the "onClick" listener method, call this new method, passing it the string variable your code copied from the text-field:
1
speakWords(words);
Step 6: Implement TTS Within the Class
2
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
You also need to implement one more interface, so alter your class declaration outline to add "OnInitListener" as in the following example:
1
public class SpeakingAndroid extends Activity implements OnClickListener, OnInitListener
Step 7: Check for TTS Data
1
private int MY_DATA_CHECK_CODE = 0;
Add the following import statement at the top of the class:
1
import android.content.Intent;
In the "onCreate" method, add the following:
1
2
3
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
Step 8: Create a TTS Instance
1
private TextToSpeech myTTS;
Add the "onActivityResult" to your class as follows:
01
02
03
04
05
06
07
08
09
10
11
12
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
Step 9: Provide the onInit Method
1
import java.util.Locale;
Add the "onInit" method to the class as follows:
1
2
3
4
5
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
myTTS.setLanguage(Locale.US);
}
}
This code checks that the TTS resource is successfully instantiated, then sets a US English Locale for the speech operations. You can optionally output an error message for users if the TTS does not successfully instantiate by adding the following after the "if" block:
1
2
3
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).
show();
}
If you use this code you will also need to import the Toast class by adding the following statement at the top of your file:
1
import android.widget.Toast;
Your app can carry out checks on the user device, such as available languages, as in the following extended version of the statement creating the TTS object inside the first conditional statement:
1
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
Step 10: Speak!
1
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
There are lots of options here in terms of how your app handles speech. This code instructs the app to speak the text string immediately. If you want to add consecutive speech operations, you can instruct the app to wait until any current speech operations finish by adding your new speech item to a queue, as follows:
1
myTTS.speak(speech, TextToSpeech.QUEUE_ADD, null);
Once your class is finished with the TTS, you can optionally shut it down as follows:
1
myTTS.shutdown();
Don't include this line if you want your users to be able to make the app speak more than once.
Conclusion
To see how all of these elements fit together, here is the complete class declaration:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import java.util.Locale;
import android.widget.Toast;
public class SpeakingAndroid extends Activity implements OnClickListener,
OnInitListener {
//TTS object
private TextToSpeech myTTS;
//status check code
private int MY_DATA_CHECK_CODE = 0;
//create the Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get a reference to the button element listed in the XML layout
Button speakButton = (Button)findViewById(R.id.speak);
//listen for clicks
speakButton.setOnClickListener(this);
//check for TTS data
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}
//respond to button clicks
public void onClick(View v) {
//get the text entered
EditText enteredText = (EditText)findViewById(R.id.enter);
String words = enteredText.getText().toString();
speakWords(words);
}
//speak the user text
private void speakWords(String speech) {
//speak straight away
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
//act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
}
else {
//no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.
ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
//setup TTS
public void onInit(int initStatus) {
//check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...",
Toast.LENGTH_LONG).show();
}
}
}
Refer this link for offline Voice
http://appcrawlr.com/android/offline-voice-diction-trial
Hope this will help to you
Thank u
01
02
03
04
05
06
07
08
09
10
11
12
13
14
<TextView android:id="@+id/intro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter some text:"
/>
<EditText android:id="@+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
/>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.