Android:播放由TTS

时间:2015-12-28 11:56:12

标签: android audio text-to-speech

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

当我尝试播放时,应用程序崩溃了

  

致命的例外:主要                                                                                   过程:com.burfdevelopment.lejostoandroid,PID:6539                                                                                     java.lang.IllegalStateException                                                                                         在android.media.MediaPlayer._setDataSource(本机方法)                                                                                         在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1133)                                                                                         在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1118)                                                                                         在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1097)                                                                                         在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1046)                                                                                         在   com.burfdevelopment.lejostoandroid.MainActivity.speakWords(MainActivity.java:351)                                                                                         在   com.burfdevelopment.lejostoandroid.MainActivity.onInit(MainActivity.java:436)                                                                                         在   android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:814)                                                                                         在android.speech.tts.TextToSpeech.-wrap4(TextToSpeech.java)                                                                                         在   android.speech.tts.TextToSpeech $连接$ SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:2174)                                                                                         在   android.speech.tts.TextToSpeech $连接$ SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:2168)                                                                                         在android.os.AsyncTask.finish(AsyncTask.java:651)                                                                                         在android.os.AsyncTask.-wrap1(AsyncTask.java)                                                                                         在   android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:668)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                         在android.os.Looper.loop(Looper.java:148)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                         在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(soundFilename);

1 个答案:

答案 0 :(得分:2)

尝试使用以下代码。

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