简体   繁体   中英

TextToSpeech SynthesizeToFile android function java

I am trying to save the output of TextToSpeech into mp3 file or wav file, I have tested different codes but it never work

The first code i tried:

File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File(root.getAbsolutePath() + "/me");
    dir.mkdirs();
    File file = new File(dir, "myData.wav");
    Log.d("Debug ","Path : "+file.getAbsolutePath());
    HashMap<String, String> params = new HashMap<>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, textToRead);
    if(textToSpeech.synthesizeToFile(textToRead,params,file.getAbsolutePath()) == textToSpeech.ERROR){
        Log.d("Debug ","file write fail ");
    }else {
        Log.d("Debug ","file write done ");
    }

but always the result is "file write fail"

i tried to enter hard coded path to sdcard and it didn't work

textToSpeech.synthesizeToFile(textToRead,params,"/sdcard/Android/data/com.orbshine.orbshine/data.wav")

According to the official documentation , by default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage.

You can either:

  1. Target Android 9 (API level 28) or lower (temporary workaround)
  2. Opt-out of scoped storage (temporary workaround)
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
     Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>
  1. Migrate your code in order to use the new scoped storage features. This is the recommended solution, as it is the only one that will keep working when targeting API 30 and higher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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