简体   繁体   English

未创建Android TextToSpeech.synthesizeToFile()文件

[英]Android TextToSpeech.synthesizeToFile() file is not created

I am trying to implement a pause and play function to some text using tts and MediaPlayer. 我正在尝试使用tts和MediaPlayer对某些文本实现暂停和播放功能。 However, I can't seem to be able to create a .wav file using the synthesizeToFile function. 但是,我似乎无法使用synthesizeToFile函数创建.wav文件。

I already added the required permission to the xml file: 我已经为xml文件添加了所需的权限:

<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: 这就是我创建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? 有任何想法吗?

The method synthesizeToFile is asynchronous thus you should do the checking 方法synthesizeToFile是异步的,因此你应该进行检查

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 onUtteranceCompletedListenerUtteranceProgressListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM