this is my first post here. I'm new in Android Programming. I want to create an app where I can save the output of the text to speech into an audio file to my database. I've heard about synthesizeToFile() but It's not it.
Asked
Active
Viewed 9,584 times
4 Answers
9
synthesizeToFile() should create a wav (which you can decode and send to your db or save as a file or whatever you're doing with it), and you can play it back using Nitesh's code.
From http://android-developers.blogspot.fi/2009/09/introduction-to-text-to-speech-in.html:
HashMap<String, String> myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/wakeUp.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, wakeUpText);
mTts.synthesizeToFile(wakeUpText, myHashRender, destFileName);
Once you are notified of the synthesis completion, you can play the output file just like any other audio resource with android.media.MediaPlayer.

Juuso Ohtonen
- 8,826
- 9
- 65
- 98

h4rpur
- 341
- 2
- 6
1
Use this code and get the mp3 file acess from the assets folder and try this code.
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this,R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.stop();
}
});

Sergey Glotov
- 20,200
- 11
- 84
- 98

Nitesh Khosla
- 875
- 8
- 20
0
mTTS =new TextToSpeech(this, new TextToSpeech.OnInitListener()
private String mAudioFilename = "";
private final String mUtteranceID = "totts";
@Override
public void onInit(int status) {
bsave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveToAudioFile(mEditText.getText().toString().trim());
}
});
CreateFile();
}
private void CreateFile() {
// Perform the dynamic permission request
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
// Create audio file location
File sddir = new File(Environment.getExternalStorageDirectory() + "/My File/");
sddir.mkdir();
mAudioFilename = sddir.getAbsolutePath() + "/" + mUtteranceID + ".wav";
}
private void saveToAudioFile(String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTTS.synthesizeToFile(text, null, new File(mAudioFilename), mUtteranceID);
Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
} else {
HashMap<String, String> hm = new HashMap();
hm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,mUtteranceID);
mTTS.synthesizeToFile(text, hm, mAudioFilename);
Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
}
}
});
-
This code helps you to save the audio of your TTS output. But the problem is when to save the output for the second time it over writes on the first audio file . If someone knows the solution for this problem kindly post your code for this. – Ragul Vinay Apr 03 '20 at 19:43
0
You should be saved in the tts
file assets folder.

Taryn
- 242,637
- 56
- 362
- 405

Nitesh Khosla
- 875
- 8
- 20
-
thanks, got it. but how can I make the tts output into an audio file?(i.e .wav, .mp3...) any ideas? – elle yang Feb 10 '12 at 15:17
-
you should down load the mp3 file anp put it into assets file and then acess it.I am send your code you use it. – Nitesh Khosla Feb 10 '12 at 15:26