How do I play sounds on an iOS device?
In this lesson we'll look at how to play an audio file from a LiveCode stack running on a mobile device. Well focus on how to add sounds to alerts and the use of the 'play' command.
You can download the sample sound file from this url: https://tinyurl.com/yde5r4z6
Create a basic stack with a 'play' button
1) Create a new stack for iPhone 5
2) Drag on two new buttons
3) Name them 'Beep' and 'Play'
Lets start with a simple alert
Support has been added for the beepSound and beep commands. These hook into iPhoneOS's standard PlayPlayerSound support.
To specify a sound to be played as the system sound, use the beepSound global property. This should be set to the filename of the sound to use when beep is executed. If you want no sound to play when using beep, simply set the beepSound to empty.
To perform a system alert, use the beep command. If no sound has been specified via the beepSound global property, the engine will request a vibration alert.
Note: The iPhone has no default system alert sound so if a sound is required one must be specified by using the beepSound. The action of beep is controlled by the system and depends on the user's preference settings. In particular, a beep will only cause a vibration if the user has enabled that feature. Similarly, a beep will only cause a sound if the phone is not in silent mode.
Add the following code to your 'beep' button:
on mouseUp set the beepsound to "empty" beep end mouseUp
Note: You will need to deploy this to your iPhone in order to test it. This script will cause your iPhone to vibrate if you have that enabled.
Getting our sounds onto the device
Both the 'beepsound' and the play command require the path of the audio file so we need to add a sound file to our app. You can add files to your iOS app package via the standalone builder:
2) Standalone application settings
3) Go to the 'Copy Files' pane
4) Click 'Add File...'
5) Now just browse to your sound file and click OK. Thie file will appear in the list and be added to your app bundle when you save the standalone.
Adding sound to our 'beep' button
Now that we have a sound file ready for bundling into our app we can add the script to use it.
on mouseUp set the beepsound to specialFolderPath("resources") & "/sound.mp3" beep end mouseUp
* To test you will need to deploy this to your device.
Playing audio files on the iPhone - The basics
Basic support for playing sounds has been added using a variant of the play command. A single sound can be played at once by using:
play soundFile [ looping ]
Executing such a command will first stop any currently playing sound, and then attempt to load the given sound file. If looping is specified the sound will repeat forever, or until another sound is played.
If the sound playback could not be started, the command will return “could not play sound” in the result.
To stop a sound that is currently playing, simply use:
The volume at which a sound is played can be controlled via the playLoudness global property.
The overall volume of sound playback depends on the current volume setting the user has on their
This feature uses the built-in sound playback facilities on the iPhone (AVAudioPlayer, to be
specific) and as such has support for a variety of formats including AIFF and MP3's.
Important: The iPhone simulator appears to have somewhat buggy support for sound playback via
AVAudioPlayer – it will intermittently fail for no reason. Therefore, if you are using the play
command be sure to test your application on a real device.
Using the 'play' command
To play our sound file add the following code to your 'play' button:
on mouseUp play specialFolderPath("resources") & "/sound.mp3" end mouseUp