Maniruzzaman Akash's Blog

Maniruzzaman Akash, A programmer, A web programmer, a helpful person to share knowledge and everything..

  • Home
  • Programming
    • C Programming
    • Java Programming
    • C++ Programming
    • C# Programming
    • Python Programming
    • Data Structure
  • Web
    • HTML
    • CSS
    • Javascript
    • PHP
    • AJAX
    • XML
  • FrameWork
    • Bootstrap(CSS)
    • Laravel 5(PHP)
  • Database
    • MySQL
    • Oracle
    • SQL Server
  • Android
    • Android Development
  • Mathematics
    • Numerical Methods
  • Others
    • My Articles
    • Download Ebooks
    • Mathematics
    • Difference Between
  • Contact
Android Developement

How to make a simple android text to speech converter app

Thursday, February 2, 2017 By Maniruzzaman Akash 0 Comments
In this tutorial, I'll show you how to make a simple android app of  text to speech converter on some languages..

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:





Android Developement
Share:

Maniruzzaman Akash
Maniruzzaman Akash, an enthusiastic programmer, a web developer

Related Articles


0 comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom )

Popular Posts

  • Numerical Methods 20 Multiple Choice Questions and Answers
  • Consider a hypothetical 32-bit microprocessor having 32-bit instructions: Solutions
  • List and briefly define two approaches to dealing with multiple interrupts
  • The hypothetical machine has two I/O instructions: 0011= Load AC fro I/O 0111= Store AC to I/O Solutions
  • What are the characteristics of Digital IC's?-Solution
  • Mid Square Method Code implementation in C and MatLab
  • List and briefly define the possible states that define an instruction execution
  • BFS, DFS, DLS in Tree implementation in C
  • Download Laravel Offline Documentation as HTML
  • Simpson's 1/3 Code in Matlab

Category

Advanced PHP Android Developement Articles Artificial Intelligenece Blogger Tips Blogging Career Bootstrap Offline Documentation Bootstrap Templates C Programming Computer Architecture Data Structure Difference Between Download Documentation Download Ebook Download Free Blog Template Earning Money Electrical Electronics Guest Posts HTML Java Programming Laravel Laravel Bangla Tutorial MatLab Code My Videos MySQL Database Numerical Methods Offline Documentation Recent Topics Simulation and Modeling Unity Game Development Web Design Web Development

LIKE ME ON FACEBOOK

TAGS

  • Advanced PHP (3)
  • Android Developement (5)
  • Articles (6)
  • Artificial Intelligenece (3)
  • Blogger Tips (5)
  • Blogging Career (1)
  • Bootstrap Offline Documentation (1)
  • Bootstrap Templates (1)
  • C Programming (14)
  • Computer Architecture (5)
  • Data Structure (11)
  • Difference Between (1)
  • Download Documentation (2)
  • Download Ebook (3)
  • Download Free Blog Template (2)
  • Earning Money (1)
  • Electrical Electronics (1)
  • Guest Posts (1)
  • HTML (4)
  • Java Programming (2)
  • Laravel (3)
  • Laravel Bangla Tutorial (1)
  • MatLab Code (2)
  • My Videos (3)
  • MySQL Database (7)
  • Numerical Methods (9)
  • Offline Documentation (1)
  • Recent Topics (1)
  • Simulation and Modeling (2)
  • Unity Game Development (3)
  • Web Design (3)
  • Web Development (1)

Join Google+

Maniruzzaman Akash
View my complete profile

Join With Me

Site Visitors

Best Sites For a programmer

  • URI Online Judge Solution By Me
  • StackOverFlow
  • W3 School
  • Git Hub - Store your Every Document

Popular Posts

  • What are the characteristics of Digital IC's?-Solution
  • The hypothetical machine has two I/O instructions: 0011= Load AC fro I/O 0111= Store AC to I/O Solutions
  • Consider a hypothetical 32-bit microprocessor having 32-bit instructions: Solutions
  • Numerical Methods 20 Multiple Choice Questions and Answers
  • How to import Excel,CSV file in Laravel And insert data in database
  • Mid Square Method Code implementation in C and MatLab
  • Depth First Search DFS code using Binary Tree in C language
  • Numerical method Codes simple MatLab implementation
  • List and briefly define two approaches to dealing with multiple interrupts
  • ফিফা ওয়ার্ল্ড কাপ ২০১৮ শিডিউল এবং সমস্ত আপডেট- FIFA World Cup 2018 - Bangladesh Time Schedule

Earn Money From Your site

Translate To your language

Categories

Advanced PHP (3) Android Developement (5) Articles (6) Artificial Intelligenece (3) Blogger Tips (5) Blogging Career (1) Bootstrap Offline Documentation (1) Bootstrap Templates (1) C Programming (14) Computer Architecture (5) Data Structure (11) Difference Between (1) Download Documentation (2) Download Ebook (3) Download Free Blog Template (2) Earning Money (1) Electrical Electronics (1) Guest Posts (1) HTML (4) Java Programming (2) Laravel (3) Laravel Bangla Tutorial (1) MatLab Code (2) My Videos (3) MySQL Database (7) Numerical Methods (9) Offline Documentation (1) Recent Topics (1) Simulation and Modeling (2) Unity Game Development (3) Web Design (3) Web Development (1)

Subscribe To This Site To Get Latest Article On Programming or web

Posts
Atom
Posts
Comments
Atom
Comments
© 2016 Maniruzzaman Akash's Blog | All rights reserved
Developed By Maniruzzaman Akash Created By Responsive Blogger Templates | Distributed By Gooyaabi Templates