Android Question tts synthesizeToFile() save audio

Status
Not open for further replies.

Douglas Farias

Expert
Licensed User
Longtime User
Hi all.
how can i put the synthesizeToFile() event on tts lib?
i want to save a audio file and this is the native method.

it already is on tts lib?

some functions i can get with JavaObject like isSpeaking event.
B4X:
Dim jTTS As JavaObject = tts1 'not TTS
    If jTTS.RunMethod("isSpeaking", Null) = False Then

and get max getMaxSpeechInputLength
B4X:
       Dim jTTS As JavaObject = tts1
       suportado = jTTS.RunMethod("getMaxSpeechInputLength", Null)

but how to use synthesizeToFile usign JavaObject or reflection? its possible?

http://developer.android.com/reference/android/speech/tts/TextToSpeech.html

synthesizeToFile(String text, HashMap<String, String> params, String filename)

i know, i can try record a audio with AudioStreamer(can not work on some devices).
but if exist a native function to this on tts why not use?

its possible use this function with JavaObject or reflection libs? if yes how?

thx all.
 
Last edited:

rboeck

Well-Known Member
Licensed User
Longtime User
I have a question related also to Text To Speach: Is is possible now to switch between different speach engines without user intervention? I use primary google text to speach, but some languages are only available with svox engine. I hope that newer android api's can switch between speach engines?!
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
Try this:
B4X:
Dim jo As JavaObject = tts1
jo.RunMethod("synthesizeToFile", Array("One two three", Null, File.Combine(File.DirRootExternal, "1.wav")))
Hi @Erel
sorry for the delay on the response.
i m tested your example code today and i got this error.

Installing file.
PackageAdded: package:leitor.df.texto
Copying updated assets files (18)
** Activity (main) Create, isFirst = true **
Class not found: leitor.texto.like.speakbutton, trying: leitor.df.texto.speakbutton
** Activity (main) Resume **
BR
Error occurred on line: 1232 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
at leitor.df.texto.main._btshare_click(main.java:2409)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5246)
at android.view.View$PerformClick.run(View.java:21200)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
at android.speech.tts.TextToSpeech.synthesizeToFile(TextToSpeech.java:1918)
... 25 more

reading the tts by android documentation this show
synthesizeToFile(String text, HashMap<String, String> params, String filename)
This method was deprecated in API level 21. As of API level 21, replaced by synthesizeToFile(CharSequence, Bundle, File, String).

my device its api 22 and i compiled the app with 23

the new code for synthesizeToFile its
synthesizeToFile(CharSequence text, Bundle params, File file, String utteranceId)
Synthesizes the given text to a file using the specified parameters.

what this changes at your example code?
i think your code its working and its correct for android < 21, but how to make this new synthesizeToFile to android > 21 ?
CharSequence, Bundle etc...


i m tryed this
B4X:
        Dim jo As JavaObject = tts1
        jo.RunMethod("synthesizeToFile", Array(s, Null, File.Combine(FP, "1.wav"),"casa"))

but return error
** Activity (main) Create, isFirst = true **
Class not found: leitor.texto.like.speakbutton, trying: leitor.df.texto.speakbutton
** Activity (main) Resume **
BR
** Activity (main) Resume **
WakeLock already held.
Error occurred on line: 1232 (Main)
java.lang.RuntimeException: Method: synthesizeToFile not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:128)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:742)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5246)
at android.view.View$PerformClick.run(View.java:21200)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

thx again
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The documentation says that the Map parameter can be null. Based on the error it cannot be null.

This code works:
B4X:
Sub tts1_Ready (Success As Boolean)
   Log(Success)
   Dim jo As JavaObject = tts1
   Dim m As JavaObject
   m.InitializeNewInstance("java.util.HashMap", Null)
   jo.RunMethod("synthesizeToFile", Array("a b c", m, File.Combine(File.DirRootExternal, "1.wav")))
End Sub

Sub Activity_Click
   Dim mp As MediaPlayer
   mp.Initialize
   mp.Load(File.DirRootExternal, "1.wav")
   mp.Play
End Sub
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
lol many thx erel i will test. thx again.
you have plans to put this on TTS lib?
getMaxSpeechInputLength and
isSpeaking event?
 
Upvote 0
Status
Not open for further replies.
Top