Search code examples
androidaudiotext-to-speech

Android : Play WAV created by TTS


So I am writing an app that creates a WAV file of some spoken Text via the TextToSpeech. I have confirmed the file is created successfully

When I then try to play it, the app crashes with

FATAL EXCEPTION: main Process: com.burfdevelopment.lejostoandroid, PID: 6539 java.lang.IllegalStateException at android.media.MediaPlayer._setDataSource(Native Method) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1133) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1118) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1097) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1046) at com.burfdevelopment.lejostoandroid.MainActivity.speakWords(MainActivity.java:351) at com.burfdevelopment.lejostoandroid.MainActivity.onInit(MainActivity.java:436) at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:814) at android.speech.tts.TextToSpeech.-wrap4(TextToSpeech.java) at android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:2174) at android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:2168) at android.os.AsyncTask.finish(AsyncTask.java:651) at android.os.AsyncTask.-wrap1(AsyncTask.java) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Code

private void speakWords(String speech) {
        //implement TTS here
        if (speech != null && speech.length() > 0) {

            //
            HashMap<String, String> myHashAlarm = new HashMap<String, String>();
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "1");
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");

            String root = Environment.getExternalStorageDirectory().toString();

            soundFilename = root + "/simon.wav";
            soundFile = new File(soundFilename);
            if (soundFile.exists())
                soundFile.delete();

            if(myTTS.synthesizeToFile(speech, myHashAlarm, soundFilename)== TextToSpeech.SUCCESS) {

                try {
                    mMediaPlayer.setDataSource(soundFilename);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                Toast.makeText(getBaseContext(),"Oops! Sound file not created",Toast.LENGTH_SHORT).show();
            }

        }
    }

I can play the file on the actual device via FileManager. I have tried playing it later on, I have tried just playing the file. The datasource line is where it crashes.

mMediaPlayer.setDataSource(soundFilename);


Solution

  • Try using the following code.

    // Make sure you are writing access permission in Manifest.
    soundFilename = Environment.getExternalStorageDirectory() + "/simon.wav";
    
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(soundFilename);
    
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
    {
        @Override
        public void onPrepared(MediaPlayer mp)
        {
            mMediaPlayer.start();
        }
    });
    mMediaPlayer.prepareAsync();