Office of Digital Humanities
Back     BYU LiveCode Lessons Gateway

Sound Playback on Mobile Devices

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.

Supported Audio Formats

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.)

Simple Playback

For playing back 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") & slash & "sounds" & slash & "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

Multi-channel sound playback

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.) 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.

(Note: Before version 5.5, LiveCode only supported multi-channel playback on iOS. Starting with v. 5.5, LiveCode includes multi-channel audio support for Android. In these examples I will illustrate using commands that have the 'mobile' prefix, such as mobilePlaySoundOnChannel. However, you should be aware that when when using LC v.5.0.2 or earlier these commands will use the 'iphone' prefix; e.g., iphonePlaySoundOnChannel.)

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:

  put specialFolderPath("engine") & "/soundfx/GospelPianoLoop.mp3" into tSound
  mobilePlaySoundOnChannel tSound, "Background", "looping"

Stopping playback: To stop playback of a sound use the mobileStopPlayingOnChannel command:

  mobileStopPlayingOnChannel "Background"

Pausing playback: To pause playback of a sound use the mobilePausePlayingOnChannel command:

  mobilePausePlayingOnChannel "Background"

Resuming playback: To resume playback of a paused sound on a specific channel use the mobileResumePlayingOnChannel command:

  mobileResumePlayingOnChannel "Background"

Setting volume: To set the volume for a sound channel use the mobileSetSoundChannelVolume command:

  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. For full details see the Mobile Development User Guide.

Back     BYU LiveCode Lessons Gateway
Maintained by Devin Asay.
Copyright © 2005 Brigham Young University.
This page last updated on February 09, 2017 10:51:52.