In this lesson, we will cover the basics of using the 'play' command to play audio, along with using the 'the sound' property. We will also show you how to include sound files in your application bundle when transferring apps onto a mobile device.
We are going to create a very simple app, with a button which when clicked, will play the desired sound. We will also have a second button which, when clicked, will stop the audio.
Step 1: Setting up the app
Firstly, we'll need a sound file to play. It is important to note at this point that audioclips in Livecode can be in either .WAV, .AIFF, or .AU format. The sound file I'm going to be using throughout this lesson is called "hello.aif", and is going to be located in the same folder as the application.
In Livecode, open a new mainstack and create a button on it, as above.
Step 2: Accessing and playing the audio file
There are two main ways we can access the audio file in Livecode: we can either reference it by it's filepath, or we can import the sound directly in to Livecode. The latter method means that we don't have to worry about the file getting lost or separated from the stack, as the file is integrated directly into the final application itself. The former method is, however, slightly more flexible in some situations and can avoid 'bloating' of the final application file.
For the sake of clarity, this lesson will reference the audioclip by filename. To play our audiofile, which is located on a folder on the desktop, open up the script editor for the 'Play' button, and enter the following:
on mouseUp play audioClip "/Users/davidwilliams/desktop/audiolesson/hello.aif" end mouseUp
Note that the filepath you should enter will be different from the above, depending on where you have stored your audio file on your computer. Alternately, you can use the specialFolderPath function to automatically create a path to certain places such as the desktop - more on this later.
Once you have entered this, click the Play button on your stack. You should hear the audio file you entered playing - if not, check to make sure that you entered the filepath correctly.
Step 3: Controlling audio with the play command
The play command can also be used to continually loop an audioclip, or stop an audioclip that is currently playing. Looping a piece of audio is simple - for example, to cause our button to loop the audio file, simply add the 'looping' argument to our existing line of code:
play audioClip "/Users/davidwilliams/desktop/audiolesson/hello.aif" looping
We can stop this loop by creating a second button, with the following code:
on mouseUp play stop end mouseUp
Click this new button, and the loop will stop. It's also worth noting that 'play empty' would have the same effect; this can be useful in some situations.
Step 4: Using the 'the sound' function
'The sound' is a function that returns the filename of currently playing audioclip (or simply the name of the audioclip if it has been imported). This has many uses, for example carrying out a specific action depending on the sound that is playing. If we add the following code to our 'Play' button, we can output the filename of the sound that is playing:
answer the sound
A feature of this function is that when there is no sound playing, the function returns 'done'. This makes for some very nice, readable code. For example, if we want to wait until the sound has finished, and then carry out an action, we can do the following in the script of our 'Play' button:
on mouseUp play audioClip "/Users/davidwilliams/desktop/audiolesson/hello.aif" wait until the sound is done answer "Sound is done!" end mouseUp
Click the play button - the sound will play, and when done, a message box will appear.
Step 5: Tips for accessing audio files on desktop and mobile devices
In cases where you don't want to import the audio files directly into your application, and particularly on mobile devices, it's rarely practical to reference the audio file by an absolute filename as we have done in the examples above. There are some built in functions in Livecode that make it easy to access audio files (and files in general) dynamically.
Firstly, there is the specialFolderPath function. This function returns the filepath to common folders such as the 'Documents' folder, or the Desktop. For example, specialFolderPath("Desktop") returns the path to the desktop on any Mac OSX machine. For a full list of the folders this function can handle, see the dictionary entry for specialFolderPath on this site, or in Livecode.
Secondly there is the 'filename of stack' function. You can use this function to get the filepath of the stack on any device. For example, the following line of code would bring up a message box with the filepath to our stack:
answer the filename of this stack
Therefore, the following code will always give us the path to the folder our stack is in, making it simple for us to access any audio files we included when buliding the app.
set the itemDelimiter to "/" put item 1 to -2 of the filename of this stack into tStackFolderPath
Using a method like this to access your files is essential on mobile devices, where the path to your stack folder will vary wildly from device to device.