Reputation: 21
I tried to use this code in my android application:
TextToSpeech mTts;
HashMap<String, String> myHashRender = new HashMap();
String textToConvert = "this is a demo for saving an mp4 file";
String destinationFileName = "/sdcard/test.mp4";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, textToConvert);
mTts.synthesizeToFile(textToConvert, myHashRender, destinationFileName);
but the methods are outdated because some methods have been changed in the api level 21 update. The TextToSpeech.synthesizeToFile() method's parameters were changed and I don't know which arguments to pass through. Also, android studio doesn't recognize what TextToSpeech.Engine is. How should I change the code above so that the entered text (in type String) is converted to an audio file? Also, am I missing anything?
Update:
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import java.io.File;
import java.util.HashMap;
public class TextToSpeechFile
{
private static String fileLocation;
private static String text;
private TextToSpeech mTts;
public TextToSpeechFile(String fLocation, String txt){
fileLocation = fLocation;
text = txt;
}
public static void createFile(){
TextToSpeech mTts;
HashMap<String, String> myHashRender = new HashMap();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
mTts.synthesizeToFile(text, myHashRender, fileLocation);
String utteranceId = "some unique id";
File file = new File(fileLocation);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
Bundle params = new Bundle(); // optional can just pass in null
params.putString("Some key", "Some value");
mTts.synthesizeToFile(text, params, file, utteranceId );
// mTts.synthesizeToFile(textToConvert, null, destinationFileName, utteranceId );
} else {
mTts.synthesizeToFile(text, myHashRender, fileLocation);
}
}
}
Now it says that mTts has not been initialized. The problem is, I don't know how to initialize a TextToSpeech object! I don't know what arguments to pass through. The class above is designed so that it can be used by different activities. The class itself is NOT an Activity class.
Thank you to those who try to stick with me. I'm a beginner in android app development, so please bear with me.
Thank you.
Upvotes: 2
Views: 4127
Reputation: 794
First you need to import this import android.speech.tts.TextToSpeech;
You still have to implement the deprecated method for backward compatibility if you are targeting device running Android pre-lollipop version. Use the below conditional method call:
final String utteranceId = "myTestingId";
File destinationFile = new File(getCacheDir(), utteranceId + ".wav");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTts.synthesizeToFile(textToConvert, null, destinationFile, utteranceId);
} else {
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
mTts.synthesizeToFile(textToConvert, param, destinationFile.getPath());
)
mTts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String s) {
if (s.equals(utteranceId)) {
// start playing the audio file defined at myTestingId.wav
}
}
});
Code to instantiate the speech engine
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
Locale locale = new Locale("en_US");
mTts.setLanguage(locale);
// speech engine is ready to rock
} else {
// speech engine initialization fail
}
}
});
Upvotes: 2