The personal wiki of ...

Exercise: Talking to the User

We're used to talking to our phone but we can make the phone talk to us. This is useful if you are creating programs for situations where your can't look at your phone or perhaps you have a visual impairment.

Computers are essentially stupid. They follow a program like a recipe and you can make it do more things by adding extra steps to the recipe.

This idea is called following a sequence.

1. Saying Hello

Use the QR code window to create a new script called cyber-speech.py.

Here is the program:

cyber-speech.py
import android
import time
droid = android.Android()
droid.ttsSpeak('Hey there')
time.sleep(3)
droid.ttsSpeak('Welcome to Android programming')
time.sleep(3)

There are two things to notice in the program:

  • droid.ttSpeak, for example droid.ttSpeak('Hey there') makes the Android say Hey there. Changing what is between the quotes changes what it says.
  • sleep, for example sleep(1) makes the Android pause 1 second before moving onto the next instruction.

TRY IT NOW

2. Making it sing

Try out this program "Gangsta Paradise".

chart-3.png

Find your own song to make it sing by adding new droid.ttsSpeak lines for each lyric.

Google your own lyrics.

Make sure that you make it pause by using sleep(3) between each line.

TRY IT NOW

ASK ANOTHER GROUP TO TRY OUT YOUR SONG

3. Change how it says it

You can change the accent by going to Settings -> Text to Speech Settings -> Language.

You can also change how fast it speaks!

Try changing the settings and running again.

TRY IT NOW

4. Hello World revisited

Remember the Hello World program from the first exercise?

First of all we are going to make a Hello World program that speaks rather than just prints your name.

To change the name of the person, you had to change the program.

What we are going to do is ask the person their name and say hello to them.

This illustrates something called a variable, used a lot in programming.

Here is the program:

input.py
import android
import time
droid=android.Android()
name=droid.dialogGetInput("What is your name","Name","Default").result
droid.ttsSpeak('Hello')
droid.ttsSpeak(name)
time.sleep(2)
droid.ttsSpeak('How are you today')
droid.ttsSpeak(name)
time.sleep(2)

The key line here is name = droid.dialogGetInput("What is your name","Name","Default").result

The dialogGetInput makes the phone display a message box asking you "What is your name".

TRY IT NOW

5. Extending Hello World.

Now here is a bit of challenge.

Make the program ask where you live and have the phone speak it

STUCK!? ASK THE TUTOR FOR A HINT

6. Phone and Sensors

Android phones come with sensors that allow you to write programs that make use of information like location or speed. The next exercise looks at using location to write a program that tracks where you have been and use the information to create a map.
Topic attachments
I Attachment Action Size Date Who Comment
chart-3.pngpng chart-3.png manage 3 K 24 Jan 2013 - 22:07 Main.ian  
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 25 Jan 2013 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated