Android Question TTS question

khwarizmi

Active Member
Licensed User
Longtime User
Hello all

Is there an event that arise in text to speech (tts library), when speech complete? or, is there any library that contains an event like that?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello all

Is there an event that arise in text to speech (tts library), when speech complete? or, is there any library that contains an event like that?
Here is a sample project that uses inline java code for TTS (inside class TTSutilities). The inline java code uses the UtteranceProgressListener and will raise an event in the TTSutilities class when speaking starts and when speaking stops. The events then uses CallSubDelayed to call subs in the Main activity when speaking starts and when speaking finishes.

You need to enable the JavaObject library to run the code

This example will only work for devices with API >= 21

Code in Main activity:
B4X:
#Region  Project Attributes
    #ApplicationLabel: TTS_Sample
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.
    Dim tts As TTSutilities

    Private Button1 As Button
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("main")

End Sub

Sub Activity_Resume
 
    tts.Initialize

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
    tts.clearTextToSpeech

End Sub

Sub Button1_Click
 
    tts.Speak("Hello. This is a sample project that uses inline java code for text to speech. It raises an event in the TTS utilities class when the speaking finishes.")
    tts.Speak("you can then use call sub delayed in the class to trigger a sub in the main activity.")
    tts.Speak("The inline java code in the TTS utilities class uses the Utterance Progress Listener to determine when the speaking finishes")
 
 
End Sub

Sub ttsStarted
 
    Log("In B4A Main and TTS started")
    Log("")
 
End Sub

Sub ttsFinished
 
    Log("In B4A Main and TTS finished")
    Log("")
 
End Sub

Code in class TTSutilities:
B4X:
'Class module
Sub Class_Globals
    Private nativeMe As JavaObject
 
End Sub
'Initializes the object.
Public Sub Initialize
    nativeMe = Me
    nativeMe.RunMethod("Initialize", Null)
End Sub

'Speak out a string of characters (a word or a sentence)
'Example:
'Dim tts As TTSUtilities
'tts.Initialize
'
'Dim mystring As String = "This is a TTS message"
'tts.speakOut(mystring)
Public Sub Speak (mystring As String)
 
    nativeMe.RunMethod("speakOut", Array (mystring))

End Sub

'Release the TextToSpeech resources
'Example:
'Dim tts As TTSUtilities
'tts.Initialize
'
'tts.clearTextToSpeech
Public Sub clearTextToSpeech
 
    nativeMe.RunMethod("clearTextToSpeech", Null)

End Sub

Private Sub tts_finished
 
 
    CallSubDelayed(Main, "ttsFinished")
 
End Sub

Private Sub tts_error
 
    Log("In B4A and TTS error")
 
End Sub

Private Sub tts_started
 
 
    CallSubDelayed(Main, "ttsStarted")
 
End Sub


#If Java

  import android.speech.tts.TextToSpeech;
  import java.util.Locale;
  import android.speech.tts.UtteranceProgressListener;


  private TextToSpeech tts;


  public void Initialize() {
        tts=new TextToSpeech(BA.applicationContext, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                //BA.Log("STATUS = " + status);
                if (status == TextToSpeech.SUCCESS) {
                    tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                        @Override
                        public void onDone(String utteranceId) {
                            //BA.Log("TTS finished");
                            //BA.Log(" ");
                            ba.raiseEventFromUI(this, "tts_finished", null);
                        }

                        @Override
                        public void onError(String utteranceId) {
                            //BA.Log("TTS error");
                            //BA.Log(" ");
                            ba.raiseEventFromUI(this, "tts_error", null);
                        }

                        @Override
                        public void onStart(String utteranceId) {
                            //BA.Log("TTS start");
                            //BA.Log(" ");
                            ba.raiseEventFromUI(this, "tts_started", null);
                        }
                    });
                } else {
                    BA.Log("Initilization Failed!");
                }
            }
        });
  }


  public void speakOut(String str) {
   
        tts.speak(str, TextToSpeech.QUEUE_ADD, null, "message");
  }


  public void clearTextToSpeech() {
      // Don't forget to shutdown tts!
      if (tts != null) {
          tts.stop();
          tts.shutdown();
      }
  }



#End If
 

Attachments

  • TTS_sample.zip
    9.1 KB · Views: 270
Last edited:
Upvote 0
Top