Android Question TTS - Save speech output to "Anywhere"

GMan

Well-Known Member
Licensed User
Longtime User
I want to save the spoken words of a TTS EditText to a file anywhere - Device, Cloud or sharing via FB.

Is that simply possible ?

Here is the depending part of the code:

B4X:
Sub btnSpeak_Click
    If EditText1.Text.Length > 0 Then
        TTS1.Speak(EditText1.Text, True)
        EditText1.SelectAll
    End If
End Sub
 

JohnC

Expert
Licensed User
Longtime User
Do a search for "synthesizeToFile" in this forum and that will give you the code to record the TTS output to a file.

Then just copy that file to the destination of choice.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
I tried this

B4X:
Dim jo1 As JavaObject = TTS1
        jo1.InitializeArray("jo1",Null)
        jo1.RunMethod("synthesizeToFile", Array("One two three", Null, File.Combine(File.DirAssets, "gong.mp3")))
but throws an error "java.land.ClassNotFoundException"
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
get rid of #2
signature is wrong in #3 (4 paratmeters expected. add an "ID")
you can't save anything to dirassets. use dirinternal or safedirexternaldefault (if you want to play result from laptop)
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Also, that method will only generate WAV files, not MP3.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thanks for your advise.
Ths is my code now (the EditText1.Text contains the Text-To-Speech).

B4X:
Dim jo1 As JavaObject = TTS1
        jo1.RunMethod("synthesizeToFile", Array(EditText1.Text, File.Combine(File.DirDefaultExternal, "gong.wav")))

But throws another error:
An error has occured in sub.main$ResumableSub_btnSpeak_Clickresume
(java line: 445)
java.lang.RuntimeException: Method:
synthesizeToFile not matched
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you're still not following the documentation. this is what you need to do:

B4X:
    Dim javavoice As JavaObject
    javavoice = tts
    Dim speechfile As JavaObject
    Dim rp As RuntimePermissions
    speechfile.InitializeNewInstance("java.io.File", Array(rp.GetSafeDirDefaultExternal(""),"talk.wav"))

    Dim success As Int = javavoice.RunMethod( "synthesizeToFile",Array(text, Null, speechfile, "1"))

the method takes 4 parameters; you're only using 3. you're missing an id (which, i think, can be null)
the method doesn't take a string as the 3rd parameter; it takes a "file"
look at the example closely. from an old app. i tried it again this morning to make sure it works. it works.
and as @JohnC mentions, the file has to be .wav. android doesn't create .mp3's (it's not open source, and google won't pay the licensing fee. android is open source.)
also, technically, the method is async, so you could use a wait for, otherwise you could easily assume the file has been saved successfully when it hasn't
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thx again - this works without errors.
B4X:
    Dim javavoice As JavaObject
    javavoice = TTS1
    Dim speechfile As JavaObject
    Dim rp As RuntimePermissions
    speechfile.InitializeNewInstance("java.io.File", Array(rp.GetSafeDirDefaultExternal( ""),"talk.wav"))

    Dim success As Int = javavoice.RunMethod("synthesizeToFile",Array(EditText1.Text, Null, speechfile, "1"))
     Log (success)


I also added this to the manifest editor:

B4X:
AddManifestText(<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="31" />
)

But: i cant found the saved file and the logging of success shows always "0"
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
0 = success. actually, "success" should be "error". error 0 means success (no error). it's a programming convention.
the file will be where you put it. i hope you put it in in dirinternal or getsafedirexternal(""), in which case, you don't need the write permission.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The file should be in a location similar to:

android/data/your_package_name/files
 
Upvote 0
Top