hello friends i had created a plugin for unity. the plugin works as creating audio files from text input. i am using default text to speech functionality of android. Sometimes it gives the output properly but sometimes it goes with default voice. how could i sort this out?...... please if anyone had already solved it then please let me know.
If this is unable to solve then please let me know. if there is way to generate audio files from any other engine in android. i am finding the solution which will work offline.
public class TextToSpeechLib extends Application {
private Context mContext;
String Error = "";
public float mSpeed=1f;
public float mPitch=1f;
public boolean _isAvatar = true;
public boolean _doEveryTime = false;
String _voiceNameAvatar = "en-in-x-ahp#male_1-local";
String _voiceNameJudge = "en-in-x-cxx#male_3-local";
public void DoLog(final Context ctx, final String message){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
}
});
}
public void setSppedAndPitch(float speed,float pitch,boolean isAvatar,boolean everyTime,String voice){
mSpeed = speed;
mPitch = pitch;
_isAvatar = isAvatar;
_doEveryTime = everyTime;
_voiceNameAvatar = voice;
}
public void textToAudio(Context ctx, String text,String fileName,String folderName){
mContext = ctx;
AsyncTextToSpeech atts = new AsyncTextToSpeech();
atts.execute(text,fileName,folderName);
}
private class AsyncTextToSpeech extends AsyncTask<String,Void,Void>{
private String msg;
private String fileName,folderName;
TextToSpeech TTS1;
@Override
protected Void doInBackground(String... strings) {
msg = strings[0];
fileName = strings[1];
folderName = strings[2];
TTS1 = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
for ( Voice tmpVoice : TTS1.getVoices()) {
if(_isAvatar) {
if (tmpVoice.getName().equals(_voiceNameAvatar)) {
TTS1.setVoice(tmpVoice);
break;
}
}else {
if (tmpVoice.getName().equals(_voiceNameAvatar)) {
TTS1.setVoice(tmpVoice);
break;
}
}
}
TTS1.setSpeechRate(mSpeed);
TTS1.setPitch(mPitch);
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,msg);
File appTmpPath = new File(folderName);
appTmpPath.mkdirs();
String tempFilename = fileName;
String tempDestFile = appTmpPath.getAbsolutePath() + "/"+ tempFilename;
final File appTmp = new File(tempDestFile);
String utteranceId=this.hashCode() + "";
TTS1.synthesizeToFile(msg,null,appTmp,utteranceId);
}
}
});
return null;
}
}
}