Speech to Text

If you are new to SL4A and scripting, take a look at this wiki entry about using SL4A with Tasker.

This profile and script make Android's speech-to-text capability available to Tasker users. It brings up a speech capture dialog, translates the speech to text, and stores the text in the clipboard.

The profile saves what was in the clipboard before the speech-to-text script runs and restores it afterwards. This isn't strictly necessary, but it's probably good practice for those times when the clipboard already contains something you need.

Long-pressing the Camera button brings up a Speech capture dialog, and then Tasker repeats what you said in its own "voice". This is not meant to be a particularly useful program. It's meant to demonstrate how to do speech-to-text and use it to run tasks.

Profile: SpeechToClip

  • Event: Button: Camera

Enter: SpeechToClip

  • A1: Variable Set [ Name:%SAVECLIP To:%CLIP ] If [ %CLIP Is Set ]
  • A2: Set Clipboard [ Text:? Add:Off ]
  • A3: Run Script [ Name:SpeechToClip.py Terminal:Off Pass Variables: ]
  • A4: Wait Until [ MS:100 Seconds:0 Minutes:0 Hours:0 Days:0 ] If [ %CLIP !~ ? ]
  • A5: Say [ Text:%CLIP Engine:Voice:com.svox.pico:eng-USA Stream: 3 …
  • A6: Set Clipboard [ Text:%SAVECLIP Add:Off ] If [ %SAVECLIP Is Set ]
  • A7: Variable Clear [ Name:%SAVECLIP ]

Save the following 3 lines of code as SpeechToClip.py in the sl4a/scripts folder:

import android
droid = android.Android()

You may also want to replace the "Wait Until" with a more nuanced wait process, since "Wait Until" may wait forever (not sure about that).

Don't want to discredit what the above says because I am by no means an experienced coder but this is what worked better for me. I used this .py file instead and it is working for me in a scenario where it sends a text of what I say.

import android
droid = android.Android()
text= droid.recognizeSpeech()

Seemed to work better for me so try that out if the first doesn't work for you.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License