Android Question TTS Settings

ALBRECHT

Active Member
Licensed User
Hello,

Please, 2 Questions about TTS settings :

1/ I've seen that there are some Voice Settings in Android Text-to-speech output , like
- 2 or 3 Female voices
- 2 or 3 Male voices
depending to the voice Language installed.
Is there a way to access to that settings by code with TTS.SetLanguage or other ?
apparently a TTS.getVoices() method could be useful and available (API level 21 and above) to gender Android TTS male or female voices

2/ On my Android 9 phone, There are Major Text-to-speech Languages already installed,
but not : Arabian and Hebreu
How to add both of them manualy
and when TTS1.SetLanguage("he","IL") or ("ar","") are false, how to suggest with a link to users to add them and install it to their phones ?

Regards
Take care of your health šŸ˜·
Michel
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example:
B4X:
Sub Process_Globals
    Private tts As TTS
End Sub

Sub Globals
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        tts.Initialize("tts")
        Wait For TTS_Ready (Success As Boolean)
        Log(Success)
        Dim jo As JavaObject = tts
        Dim voices() As Object = jo.RunMethodJO("getVoices", Null).RunMethod("toArray", Null)
        Dim ChosenVoice As Object
        For Each voice As JavaObject In voices
            Log("****")
            Dim locale As String = voice.RunMethod("getLocale", Null)
            Dim name As String = voice.RunMethod("getName", Null)
            Log("Locale: " & locale)
            Log("Name: " & name)
            If name = "en-us-x-sfg#male_1-local" Then
                ChosenVoice = voice
            End If
        Next
        jo.RunMethod("setVoice", Array(ChosenVoice))
        tts.Speak("This is a test", False)
    End If
End Sub
It works on my device however it is possible that the voice name will not be installed on all devices.

2.
B4X:
Dim in As Intent
in.Initialize("android.speech.tts.engine.INSTALL_TTS_DATA", "")
StartActivity(in)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i took erel's example and ran with it:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private tts As TTS
    Type sortable(locale As String, voice As Object)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If FirstTime Then
        tts.Initialize("tts")
        Wait For TTS_Ready (Success As Boolean)
        Log(Success)
    End If
   
   
    Dim jo As JavaObject = tts
    Dim voices() As Object = jo.RunMethodJO("getVoices", Null).RunMethod("toArray", Null)
    Dim sortlist As List
    sortlist.Initialize
   
    For Each voice As JavaObject In voices
        Dim locale As String = voice.RunMethod("getLocale", Null)
        Dim s As sortable
        s.locale = locale
        s.voice  = voice
        sortlist.Add( s )
    Next
    sortlist.SortType( "locale", True)
   
    Dim lv As ListView
    lv.Initialize("lv")
    Activity.AddView(lv, 0%x, 0%y, 100%x,100%y)
   
    For Each o As sortable In sortlist
        Dim voice As JavaObject = o.voice
        Dim name As String = voice.RunMethod("getName", Null)
        lv.AddSingleLine2( o.locale & " | " & name, voice )
    Next
End Sub

Sub lv_ItemClick (Position As Int, Value As Object)
    Dim jo As JavaObject = tts
    jo.RunMethod("setVoice", Array(Value))
    tts.Speak("This is a test of my new voice", False)
   
End Sub

finds the available voices, sorts them by locale, sticks them in a listview. pick a voice to listen to. be online.
 
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
Example:
B4X:
Sub Process_Globals
    Private tts As TTS
End Sub

Sub Globals
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        tts.Initialize("tts")
        Wait For TTS_Ready (Success As Boolean)
        Log(Success)
        Dim jo As JavaObject = tts
        Dim voices() As Object = jo.RunMethodJO("getVoices", Null).RunMethod("toArray", Null)
        Dim ChosenVoice As Object
        For Each voice As JavaObject In voices
            Log("****")
            Dim locale As String = voice.RunMethod("getLocale", Null)
            Dim name As String = voice.RunMethod("getName", Null)
            Log("Locale: " & locale)
            Log("Name: " & name)
            If name = "en-us-x-sfg#male_1-local" Then
                ChosenVoice = voice
            End If
        Next
        jo.RunMethod("setVoice", Array(ChosenVoice))
        tts.Speak("This is a test", False)
    End If
End Sub
It works on my device however it is possible that the voice name will not be installed on all devices.

2.
B4X:
Dim in As Intent
in.Initialize("android.speech.tts.engine.INSTALL_TTS_DATA", "")
StartActivity(in)
Hi. Erel thank you. I tried to get all the voices and put them into listview, but there was an error in debugging, the error pointed to "Dim voices() As Object = jo.RunMethodJO("getVoices", Null).RunMethod("toArray", Null)". I have installed all the voice of default tts engine.
 

Attachments

  • Error.txt
    2.9 KB · Views: 118
Last edited:
Upvote 0
Top