Tuesday, July 14, 2015

// //

Convert text to speech into audio android

This time we will convert the text to speech into audio...

 Here is the activity_main.xml:


   
    


The activity file:

 TextToSpeechActivity.java
import java.io.File;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TextToSpeechActivity extends Activity {

Button store, play;
EditText input;
String speakTextTxt;
TextToSpeech mTts;
HashMap myHashRender = new HashMap();
String tempDestFile ;

String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
//your path here!
File appTmpPath = new File(exStoragePath + "/media/alarms");
//String tempFilename = "tmpaudio.wav";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    
    store = (Button) findViewById(R.id.button1);
  //  play = (Button) findViewById(R.id.button2);
    input = (EditText) findViewById(R.id.editText1);
    store.setOnClickListener(new Button.OnClickListener()  {

        public void onClick(View v) {
        	
        	 String editText1 = input.getText().toString();
        	speakTextTxt = "Hello world";
            HashMap myHashRender = new HashMap();
            myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, speakTextTxt);

        
            Log.d("MainActivity", "exStoragePath : "+exStoragePath);
         
            boolean isDirectoryCreated = appTmpPath.mkdirs();
            Log.d("MainActivity", "directory "+appTmpPath+" is created : "+isDirectoryCreated);
         
            tempDestFile = appTmpPath.getAbsolutePath() + File.separator + editText1 +".mp4";
            Log.d("MainActivity", "tempDestFile : "+tempDestFile);
            new MySpeech(speakTextTxt);
            
         //   addRecordingToMediaLibrary();
        }
    });
}

class MySpeech implements OnInitListener
{

            String tts;

    public MySpeech(String tts)
    {
        this.tts = tts;
        mTts = new TextToSpeech(TextToSpeechActivity.this, this);
    }

    @Override
    public void onInit(int status) 
    {
   	 String editText1 = input.getText().toString();
        Log.v("log", "initi");
        int i = mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);
        if(i == TextToSpeech.SUCCESS)
        {

          Toast toast = Toast.makeText(TextToSpeechActivity.this, "Saved "+editText1,
                Toast.LENGTH_SHORT);
          toast.show();   
        }
        System.out.println("Result : " + i);
    }
  }




}

and don't forget to add this permission into the,

 AndroidManifest.xml



I think were done here, please send me a feedback if it doesn't workfor you...