Android : 播放由 TTS 创建的 WAV

Android : Play WAV created by TTS

所以我正在编写一个应用程序,通过 TextToSpeech 创建一些口语文本的 WAV 文件。我确认文件创建成功

当我尝试播放它时,应用程序崩溃并显示

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)

代码

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

        }
    }

我可以通过FileManager在真机上播放文件。我试过稍后播放它,我试过只播放文件。数据源行是它崩溃的地方。

mMediaPlayer.setDataSource(声音文件名);

尝试使用以下代码。

// 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();