Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

java - Android TextToSpeech.synthesizeToFile() file is not created

I am trying to implement a pause and play function to some text using tts and MediaPlayer. However, I can't seem to be able to create a .wav file using the synthesizeToFile function.

I already added the required permission to the xml file:

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This is the file creation method I am currently using:

private String envPath = Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/Download";
private Uri fileUri;

public void fileCreate() {
    String inputText = output.getText().toString();

    HashMap<String, String> myHashRender = new HashMap<String, String>();
    myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);
    Log.d(TAG, "successfully created hashmap");

    String destFileName = envPath + "/" + "tts_file.wav";

    int sr = tts.synthesizeToFile(inputText, myHashRender, destFileName);
    Log.d(TAG, "synthesize returns = " + sr);
    File fileTTS = new File(destFileName);

    if (fileTTS.exists()) {
        Log.d(TAG, "successfully created fileTTS");
    }
    else {
        Log.d(TAG, "failed while creating fileTTS");
    }

    fileUri = Uri.fromFile(fileTTS);
    Log.d(TAG, "successfully created uri link: " + fileUri.getPath());
}

This is how I create the mediaPlayer:

fileCreate();
    mp = MediaPlayer.create(this, fileUri);
    Log.d(TAG, "successfuly created mediaplayer");

    btnRead.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (mp.isPlaying()) {
                mp.pause();
                Log.d(TAG, "successfully paused");
            } else {
                mp.start();
                Log.d(TAG, "successfully started");
            }
        }

    });

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The method synthesizeToFile is asynchronous thus you should do the checking

File fileTTS = new File(destFileName);

if (fileTTS.exists()) {
    Log.d(TAG, "successfully created fileTTS");
}
else {
    Log.d(TAG, "failed while creating fileTTS");
}

in onUtteranceCompletedListener or UtteranceProgressListener


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...