How can I save TTS output in an audio file on android?

落花浮王杯 提交于 2019-11-30 15:54:56
Nitesh Khosla

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();
    }
});
h4rpur

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.

Nitesh Khosla

You should be saved in the tts file assets folder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!