Question

Im trying to save the speech to wav output file.Nothing works.It returns -1. I tried to check if storage writeable by:

    public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

it returns True.

I tried to create dir with MODE_WORLD_WRITEABLE premission and wtire to it.- doesnt work

I tried to give WRITEABLE premission in the manifest - doesnt work

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

Please help me what should i do? Thanks a lot

P.s the Play method works

Was it helpful?

Solution

What is the int returned by the TextToSpeech.synthesizeToFile?

Are you sure about the path given by Environment.getExternalStorageDirectory? You can try to set the path manually by something like /storage/sdcard0/myTts.wav and search for that file with a file manager app. I really had trouble finding my synthesized files on my phone as well, due to the wierd behaviour of android saving files to the external sd card.

Another problem i faced using Tts is the restriction to strings with maximum 4000 characters. But as long as your .speak method is working this should not be the problem.

OTHER TIPS

1)First create your path

boolean file = new File("give your path here").mkdirs();

2)Your file name

String destination_file_name = {"gave your path"}+"Your_file_name";

3)Let String Converting_Sting be your string to convert

String Converting_Sting;

4)Let get the max capacity of your text to speech engine

TextToSpeech tts;

int max_capacity_tts=tts.getMaxSpeechInputLength();

5)if your convert string length greater than max capacity you have to reduce the length

    if(Converting_Sting.length()>max_capacity_tts())
      {
       Converting_Sting.substring(0,max_capacity_tts);
      }

4)Then you can place the reduced string in the tts engine

 HashMap<String, String> params = new HashMap();
                    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, parsedText);
                    tts.synthesizeToFile(Converting_Sting,params,destinationFileName+".wav");

Dont fogot to gave write permission on manifest

Good Luck

(-1) error when you use synthesizeToFile method using targetsdkversion 29, but when you have targetsdkversion less or equals 28 it works fine pls chekc latest update

   private void makeRingtoneFilename(String texttoconvertIntoAudiFile) {

    File saveFilePath = null;
    try {
        String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/audio/ringtones/";
        File root = new File(rootPath);
        if (!root.exists()) {
            root.mkdirs();
        }
        saveFilePath = new File(rootPath + "niceSong.m4a");
        if (saveFilePath.exists()) {
            saveFilePath.delete();
        }
        saveFilePath.createNewFile();

        FileOutputStream out = new FileOutputStream(saveFilePath);

        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }



    int i1;
    if (Build.VERSION.SDK_INT >= 21) {
        //texttoconvertIntoAudiFile is the text you want to convrt into mp3
        i1 = textToSpeech.synthesizeToFile( texttoconvertIntoAudiFile, null,saveFilePath, null );
    } else {
        i1 = textToSpeech.synthesizeToFile( texttoconvertIntoAudiFile, null, String.valueOf( saveFilePath.getAbsoluteFile() ) );
    }
    Log.e( "i1", i1 + "" );


    String outPath=saveFilePath.getAbsolutePath();
    String mimeType;
    if (outPath.endsWith(".m4a")) {
        mimeType = "audio/mp4a-latm";
    } else if (outPath.endsWith(".wav")) {
        mimeType = "audio/wav";
    } else {
        // This should never happen.
        mimeType = "audio/mpeg";
    }

    String artist = "hum h yum";
    ContentValues values = new ContentValues();
    values.put( MediaStore.MediaColumns.DATA, outPath);
    values.put( MediaStore.MediaColumns.TITLE, "niceSong.m4a");
    values.put( MediaStore.MediaColumns.MIME_TYPE, mimeType);
    values.put( MediaStore.Audio.Media.ARTIST, artist);
    values.put( MediaStore.Audio.Media.IS_RINGTONE,FILE_KIND_RINGTONE);
    // Insert it into the database
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
    final Uri newUri = getContentResolver().insert(uri, values);
    setResult(RESULT_OK, new Intent().setData(newUri));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top