5.1. Sound

5.1.1. Text to Speech

Let's start with a smooth introduction to sound. Most of the latest S60 phones have text-to-speech functionality built-in, meaning that the phone can speak aloud any given string. Hopefully you can come up with some useful and amusing application for this feature.

The module named audio provides access to the text-to-speech engine. The function audio.say() takes a string as its parameter and speaks it aloud (see Example 22).

Example 5.1. Text to speech
import appuifw, audio

text = appuifw.query(u"Type a word:", "text")
audio.say(text)

The word you type in the input dialog is spoken aloud by the phone.

5.1.2. Playing MP3 Files

The S60 platform supports a wide range of sound formats, such as MP3, WAV, AMR, AAC, MIDI and Real Audio. Example 23 shows how to play a MP3 file.

Example 5.2. MP3 player
import audio

sound = audio.Sound.open("E:\\Sounds\\mysound.mp3")

def playMP3():
    sound.play()
    print "PlayMP3 returns.."

playMP3()

You must have an MP3 file named mysound.mp3 on your phone, on the memory card in this example. If the file is missing, the example raises an exception. In Chapter 6, we explain the directory hierarchy in detail, so you know where to load and save files — here we just use a standard location for sounds.

The call to the function audio.Sound.open() opens the specified file, in this case E:\Sounds\mysound.mp3 and creates an audio. Sound object (which we have called sound). When we call the function sound.play(), the MP3 file starts ...

Get Mobile Python: Rapid Prototyping of Applications on the Mobile Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.