Android Scripting Environment is Incredible: part I

what_to-say

Update As Edward Pain pointed out, the latest version of ASE (soon to be SL4A) changed the TTS and the way that dictionaries are called. I’ve updated this script to take these into account. Thanks Ed!

So I’ve been loving having the DROID around.  I don’t even mind carrying a phone in each pocket because it’s totally worth it.  My most recent obsession is the Android Scripting Environment (ASE)

Basically, it allows you to write code in high-level languages like python, bash, perl, etc, right on the device.  This is when a physical keyboard is really handy.  So I downloaded the app and started messing around with the included python scripts.  I’ve never coded anything in python before, but I have programmed in a bunch of other languages, so it really wasn’t hard at all.  By far the coolest pre-installed script was the Text-To-Speech (TTS) example.  I’m not sure what it is about making computers/devices say funny stuff that makes me laugh so much, but its really fun.

I soon noticed as I was cracking myself up over making the DROID say things like “Then I put my hands up, they’re playing my song” that I’d really like the option to repeat what it just said.  So I decided to code it.  Please be gentle on me; I do not profess to be an expert coder, I just hack stuff together till it works.  Any suggestions would be appreciated.

First thing I did was take the original code and wrap it in a while loop.  This will repeat the phrase as many times as I like, as long as the condition remains true:

[python]

"""Speak user generated text."""

__author__ = ‘ Brett Peterson <brettjpeterson@gmail.com>’

import android

droid = android.Android()

message = droid.getInput(‘TTS’, ‘What would you like to say?’).result
droid.ttsSpeak(message)

repeat = droid.getInput(‘repeat’, ‘Repeat? Y/N’).result
while repeat == "y":
droid.ttsSpeak(message)
repeat = droid.getInput(‘repeat’, ‘Repeat? Y/N’).result

[/python]

repeat

That while loop repeats the phrase as long as the user types “y”, anything else will have it move on.  So just like that, with no previous python experience and a couple google searches later, I tailored the script to fit my specific need.  I could have stopped there, but another thing that I didn’t like about the tiny original script was the fact that I would have to reset it every time I wanted to do a new one.  I decided it would be better if after I chose not to repeat the previous phrase, I could choose to do another one:

[python]

"""Speak user generated text."""

__author__ = ‘Damon Kohler <damonkohler@gmail.com> modified by Brett Peterson <brettjpeterson@gmail.com>’
__copyright__ = ‘Copyright (c) 2009, Google Inc.’
__license__ = ‘Apache License, Version 2.0′

import android

droid = android.Android()
newOne = "y"
while newOne == "y":
message = droid.getInput(‘TTS’, ‘What would you like to say?’).result
droid.ttsSpeak(message)

repeat = droid.getInput(‘repeat’, ‘Repeat? Y/N’).result
while repeat == "y":
droid.ttsSpeak(message)
repeat = droid.getInput(‘repeat’, ‘Repeat? Y/N’).result

newOne = droid.getInput(‘Have another go?’, ‘y/n’).result

[/python]

again

So I put a while loop around the whole thing and at the end just asked the user if they would like to have another go.  That’s it!  Try writing it yourself, or open up ASE and click Menu > Add > Scan barcode and scan the below file.  Enjoy

5 thoughts on “Android Scripting Environment is Incredible: part I

  1. Pingback: Android Scripting Environment is heating up

  2. Pingback: Android Python Timelapse | Submerged Spaceman

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>