LiveCode LessonsHow To - LiveCode Mobile Tasks iOS TasksHow do I play sounds on an iOS device?

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.

Attached Files

Create a basic stack with a 'play' button

Create a basic stack with a 'play' button

1) Create a new stack

2) Set its width to 320 and its hight to 460

3) Drag on two new buttons

4) 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:

1) File

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("engine") & "/sound.aiff"

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:

play empty

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

device.

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("engine") & "/sound.aiff"

end mouseUp

9 Comments

Ray Bennett

The sound file doesn't _need_ to be renamed "sound.aiff" in order to work on the iPhone, unless you're using the code snippet exactly as is. For instance, it could just as easily be "frog.aiff". Adding the sound file to the revMobile plugin and then clicking "Deploy" bundles the sound with your app.

QUESTION: is there a list of the system sounds that are available on the iPhone (for instance, the keyclick sound is one I'd like to use)?

Ray Bennett

Snooping around on the web, I found the list of sounds shown below.

In my code, I used this:
set the beepSound to "/System/Library/Audio/UISounds/ct-keytone2.caf"

and it worked - meaning that this sound played when I executed a "beep" on the iPhone.

~rb

These are the audio files in the /System/Library/Audio/UISounds folder on 3.0 firmware:

alarm.caf
beep-beep.caf - is the sound played when docking the iPhone.
begin_record.caf
begin_video_record.caf
ct-busy.caf
ct-call-waiting.caf
ct-congestion.caf
ct-error.caf
ct-keytone2.caf
ct-path-ack.caf
dtmf-0.caf - is the sound played when pressing 0 on the phone keypad.
dtmf-1.caf - is the sound played when pressing 1 on the phone keypad.
dtmf-2.caf - is the sound played when pressing 2 on the phone keypad.
dtmf-3.caf - is the sound played when pressing 3 on the phone keypad.
dtmf-4.caf - is the sound played when pressing 4 on the phone keypad.
dtmf-5.caf - is the sound played when pressing 5 on the phone keypad.
dtmf-6.caf - is the sound played when pressing 6 on the phone keypad.
dtmf-7.caf - is the sound played when pressing 7 on the phone keypad.
dtmf-8.caf - is the sound played when pressing 8 on the phone keypad.
dtmf-9.caf - is the sound played when pressing 9 on the phone keypad.
dtmf-pound.caf - is the sound played when pressing # on the phone keypad.
dtmf-star.caf - is the sound played when pressing * on the phone keypad.
end_record.caf
end_video_record.caf
jbl_ambiguous.caf
jbl_begin.caf
jbl_cancel.caf
jbl_confirm.caf
jbl_no_match.caf
lock.caf - is the sound played when pressing the Sleep/Wake button.
long_low_short_high.caf
low_power.caf
mail-sent.caf - is the sound played when sending an email.
middle_9_short_double_low.caf
new-mail.caf - is the sound played when an email is received.
photoShutter.caf - is the sound played when taking a picture.
ReceivedMessage.caf
RingerChanged.caf
SentMessage.caf
shake.caf
short_double_high.caf
short_double_low.caf
short_low_high.caf
SIMToolkitCallDropped.caf
SIMToolkitGeneralBeep.caf
SIMToolkitNegativeACK.caf
SIMToolkitPositiveACK.caf
SIMToolkitSMS.caf
sms-received1.caf - is the sound called Tri-tone, for text message receipt.
sms-received2.caf - is the sound called Chime, for text message receipt.
sms-received3.caf - is the sound called Glass, for text message receipt.
sms-received4.caf - is the sound called Horn, for text message receipt.
sms-received5.caf - is the sound called Bell, for text message receipt.
sms-received6.caf - is the sound called Electronic, for text message receipt.
sq_alarm.caf
sq_beep-beep.caf
sq_lock.caf
sq_tock.caf
Tink.caf
Tock.caf
unlock.caf - is the sound played when you "slide to unlock".
ussd.caf
Voicemail.caf - is the sound played when you receive a voicemail message.

Siri

I used this code to try to play sounds-

play URL ("Resourcex/stimuli_X/absBAM_W.wav")

The .wav file is inside a folder called 'stimuli_X' which is inside another one called 'Resourcex' which is right beside the .rev file.

The code for playing sounds works on my windows machine but when I try to play the sounds in an identical program on the Mac it just produces a kind of ‘click’ sound and does nothing else. It does not work on the iPad either.

Can you enlighten me as to what might be going on? I would really appreciate any help you could give.

Elanor Buchanan

Hi Siri

On mobile devices you need to give the full path to the sound file you want to play, rather than just a relative path, you also don’t need the url keyword.

play (specialFolderPath("engine") & "/Resourcex/stimuli_X/absBAM_W.wav")

Remember to includes your Resourcex folder in the Copy Files pane of the Standalone Application Settings too.

I hope that helps.

Elanor

Mark Smith

Would the sounds listed above work on an iPad as well?

Thanks

Hanson Schmidt-Cornelius

Hi Mark,

if you are referring to the sound file in the lesson "sound.mp3", then yes, that file will play on an iPhone and iPad.

Kind Regards,

Hanson

genie

I was able to use this play capability without any problem, but one thing...

the sound stops when the device sleeps (or auto-locks)

Is there any way that the play command can override the device sleep? i.e. it continues playing even when the screen turns black already.

Will really appreciate any insight.

Thanks,
Genie

Hanson Schmidt-Cornelius

Hi Genie,

you should be able to play audio on iOS devices if you set the session/category correctly. Have a look at the dictionary entry "iphoneSetAudioCategory". Then try setting this to "playback".

Kind Regards,

Hanson

Mel

The code suggested by Ray Bennett:

set the beepSound to "/System/Library/Audio/UISounds/ct-keytone2.caf"

does NOT work on my iPhone 5s Simulator. Nor do any of the other sounds listed such as Tink.caf and alarm.caf.

However, this code does work OK with my "copy files" imported sound "Purr.aiff":

set the beepsound to specialFolderPath("resources") & "/AUDIO/Purr.aiff"

At least it does work in the simulator; it does not work on my iPhone 5s.
________________________________

I'm using:
LiveCode Community 8.1.3
Xcode 7.2.1
iOS simulator 9.2
Mac OS 10.10.5 (Yosemite)

Add your comment

E-Mail me when someone replies to this comment