LiveCode provides some easy-to-use functionality for playing audio on mobile devices. A synopsis follows, but full details can be found in the Mobile Development User Guide in the category "Mobile Specific Engine Features" under the topic "Multi-channel sound support", and in the LiveCode Dictionary.
Both iOS and Android allow playback of several common audio formats. Here is a partial list:
iOS: AAC, MP3, AIFF, WAV (See complete list.)
Android: MP3, WAVE, MIDI, AAC (See complete list.)
By way of review, to play a single sound file the simplest method is to use the play
command. The syntax is straightforward:
play path-to-sound-file
So if you keep your sound files in a folder called "sounds" in the same folder as your app, in a script it might look something like this:
put specialFolderPath("resources") & "/sounds/boing.mp3" into tSound play tSound
(Recall that the specialFolderPath()
function returns a complete file path to various important file locations. Click to review the discussion on specialFolderPath
.)
Stopping playback of the play command is also very simple:
play empty
While the play
command works well for playing one sound at a time, there are times when you may want to play more than one audio file simultaneously; for example, you may want to play an underlying music track or ambient sound track as well as appropriate sound effects. (Think Angry Birds or Mario Bros.) Another thing you cannot do with the play
command is pause and resume sound playback. For these more advanced capabilities LiveCode provides a complete set of sound channel playback commands, messages and functions.
Some practical examples follow. For full details see the section entitled "Multi-channel sound support" in the Mobile Development User Guide.
Playing a sound: To play a sound use the mobilePlaySoundOnChannel
command. The syntax for this command is:
mobilePlaySoundOnChannel sound, channel, type
Where:
sound is the path to the sound file to be played.
channel is a text string that you make up, naming the channel to play the sound on.
type is one of the following strings:
- "now" -The sound is played immediately, replacing any current sound (and queued sound) on the channel.
- "next" -The sound is queued to play immediately after the current sound. If no sound is playing the sound is prepared to play now, but the channel is immediately paused. This allows the sound to be prepared in advance of it being needed.
- "looping" -The sound is played immediately, replacing any current sound (and queued sound) on the channel and is looped indefinitely.
Some examples:
put specialFolderPath("resources") & "/soundfx/GospelPianoLoop.mp3" into tSound mobilePlaySoundOnChannel tSound, "Background", "looping" --> plays repeatedly until stopped put specialFolderPath("resources") & "/soundfx/pop.wav" into tSound2 mobilePlaySoundOnChannel tSound2, "fx", "now" --> plays immediately
Stopping playback: To stop playback of a sound use the mobileStopPlayingOnChannel
command, followed by the channel name that you want to stop:
mobileStopPlayingOnChannel "Background"
Pausing playback: To pause playback of a sound use the mobilePausePlayingOnChannel
command, followed by the channel name that you want to pause:
mobilePausePlayingOnChannel "Background"
Resuming playback: To resume playback of a paused sound on a specific channel use the mobileResumePlayingOnChannel
command, followed by the channel name that you want to un-pause:
mobileResumePlayingOnChannel "Background"
Setting volume: To set the volume for a sound channel use the mobileSetSoundChannelVolume
command, followed by the channel name and the volume level:
mobileSetSoundChannelVolume "Background", 70 # Note: 0 is no volume; 100 is full volume
There are several other commands and functions that allow you to check the volume for a specific sound channel, discover whether sound is currently playing on a channel, and which sound channels currently exist. There is even a message that gets sent when an audio clip finishes playing. For full details see the Mobile Development User Guide.