Let's create a android project called Text2Speech with MainActivity.
App is look like this->
And in res/layout folder activity_main.xml file is:
<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:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ccc" android:gravity="center" android:padding="10sp" android:text="@string/textView_text" android:textSize="20sp" /> <EditText android:id="@+id/edtxt_write" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/write_text" /> <Button android:id="@+id/btnListen" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#5ba" android:ellipsize="end" android:gravity="center" android:text="@string/listen_text" /> </LinearLayout>
And for that Output is look like the above image of app.
For the layout file in res/valuesfolder strings.xml file is:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Text 2 Speech</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="menu_exit">Exit</string> <string name="listen_text">Listen Now</string> <string name="write_text">Write your text here..</string> <string name="textView_text">Write your text in the edit text to speech..</string> </resources>
And now our layout is complete for android text to speech converter apps.Now go to coding in src/package../MainActivity.java code is:
package com.akash.text2speech; import java.util.Locale; import org.w3c.dom.Text; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText editextWrite; Button btnListenNow; private TextToSpeech speech; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editextWrite = (EditText) findViewById(R.id.edtxt_write); btnListenNow = (Button) findViewById(R.id.btnListen); //Setup speech object speech = new TextToSpeech(getApplicationContext(), new OnInitListener() { @Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { speech.setLanguage(Locale.UK); } } }); btnListenNow.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // Hide Keyboad from display InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(editextWrite.getWindowToken(), 0); String Speak = editextWrite.getText().toString(); Toast.makeText(getApplicationContext(), "Speaking Text is : "+Speak, Toast.LENGTH_LONG).show(); speech.speak(Speak, TextToSpeech.QUEUE_FLUSH, null); } }); } @Override protected void onPause() { if (speech != null) { speech.stop(); speech.shutdown(); } super.onPause(); } @Override public void onBackPressed() { exitAlert(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.menu_exit) { exitAlert(); } return super.onOptionsItemSelected(item); } /** * Exit Alert Function * * @return void -> Make an exit Alert */ public void exitAlert() { AlertDialog.Builder alertBuilder =new AlertDialog.Builder(MainActivity.this); alertBuilder.setTitle("Warning"); alertBuilder.setMessage("Do you want to close the app?"); alertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }); alertBuilder.setNegativeButton("No", null); alertBuilder.show(); } }
And our Manifest.xml file is:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.akash.text2speech" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.akash.text2speech.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now starting to code demonstration:
in this line
import android.speech.tts.TextToSpeech;we import TextToSpeech class of android which convert text to speech.
In this line,
private TextToSpeech speech;
Create a TextToSpeech object named speech for future reuse it.
And in this line we initialize the speech object and start it, just simple code:
//Setup speech object speech = new TextToSpeech(getApplicationContext(), new OnInitListener() { @Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { speech.setLanguage(Locale.UK); } } });
In this line of code create Initializing listener to initiate the TextToSpeech class and we have set language to UK in this line-
speech.setLanguage(Locale.UK);You cans set some other built in language and check that also pressing control+space.
And when onclick the button just start the speech
@Override public void onClick(View arg0) { // Hide Keyboad from display InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(editextWrite.getWindowToken(), 0); String Speak = editextWrite.getText().toString(); Toast.makeText(getApplicationContext(), "Speaking Text is : "+Speak, Toast.LENGTH_LONG).show(); speech.speak(Speak, TextToSpeech.QUEUE_FLUSH, null); }First hide the keyboard from the apps and then make a Speak string which will be retrieved from the edittext, then in this line:
speech.speak(Speak, TextToSpeech.QUEUE_FLUSH, null);.QUEUE_FLUSH function makes the speech and takes its as a queue of every letter and just make a flush sound of that word.
We can learn more about the function in android developer site.
Thats all of this simple android text to speech converter app. And other thing is the same as all of the apps that means when backpressed an alert dialog will be open to saying if you want to exit or not.
You can download this android text to speech converter app from dropbox. If any advertise occur, please click Skip add
Download source code:
0 comments:
Post a Comment