Skip to content Skip to sidebar Skip to footer

How Can I Save Tts Output In An Audio File On Android?

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

Solution 1:

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 = newHashMap();
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.

Solution 2:

Use this code and get the mp3 file acess from the assets folder and try this code.

mMediaPlayer = newMediaPlayer();
mMediaPlayer = MediaPlayer.create(this,R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(newOnCompletionListener() {                   
    @OverridepublicvoidonCompletion(MediaPlayer mp) {
        mMediaPlayer.stop();
    }
});

Solution 3:

You should be saved in the tts file assets folder.

Solution 4:

mTTS =new TextToSpeech(this, new TextToSpeech.OnInitListener()

privateString mAudioFilename = "";
        private final String mUtteranceID = "totts";
        @OverridepublicvoidonInit(int status) {
            bsave.setOnClickListener(newView.OnClickListener() {
                publicvoidonClick(View v) {
                    saveToAudioFile(mEditText.getText().toString().trim());
                }
            });

            CreateFile();
        }



        privatevoidCreateFile() {
                // Perform the dynamic permission requestif (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) requestPermissions(newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);

                // Create audio file locationFile sddir = newFile(Environment.getExternalStorageDirectory() + "/My File/");
                sddir.mkdir();


                mAudioFilename = sddir.getAbsolutePath() + "/" + mUtteranceID + ".wav";


            }



        privatevoidsaveToAudioFile(String text) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mTTS.synthesizeToFile(text, null, newFile(mAudioFilename), mUtteranceID);
                Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
            } else {
                HashMap<String, String> hm = newHashMap();
                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();
            }



        }
    });

Post a Comment for "How Can I Save Tts Output In An Audio File On Android?"