Play Sounds
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.
Lee
How do you import the song into the application?
Elanor Buchanan
Hi Lee
You can import the song into the application by including it in the Copy Files pane of the Standalone Application Settings.
On mobile devices you can construct the path of the music file using
specialFolderPath("engine") & "/" & tFileName
I hope that helps.
Kind regards
Elanor
Sot
How do I play an audio file from a url (stored somewhere in the web)?
Hanson Schmidt-Cornelius
Hi Sot,
you can play an audio file from the internet by using a player object. You can find more information about this in the LiveCode Dictionary if you search for the term "player"
Kind Regards,
Hanson
Tom
Hi,
I am developing for Android and I have tried the "play Soundfile" object using both [play filename] and [play audioclip filename], where filename is the correct character string full filename. I also have used the [play stop] command. The play command causes only loud static and the play stop turns it off. Using the standalone Windows Media Player the file plays properly. I get this result both with wma file type and with mp3 file type.
I also tried using the Quicktime player from the tools palette but, when I assign a file with filetype .wma the start/stop bar of the player disappears and there is no way to start the player. If I assign a .mov file to the QTP and expand the player box large enough to accomodate the video, the video does play (although haltingly).
I really need the audio play function with .wma or .mp3 files for my app.
Can you help?
Hanson Schmidt-Cornelius
Hi Tom,
Android supports mp3 files but not wma files. There are a number of reasons why the mp3 files may not be playing. In particular, check that the file path uses the appropriate case. The Android Filesystem is case sensitive. Also ensure that you use the forward-slash "/" in your path name. Also try using "specialFolderPath" to point to the root of the path where you store the mp3 files.
Kind Regards,
Hanson
Butch Hungo
What I'm not seeing is how to use media stuff (like getting the duration of an audio clip) from a clip that is loaded into a stack rather than being loaded from an external file. The commands all seem to be set up for external files. Is there some documentation explaining this?
Hanson Schmidt-Cornelius
Hi Butch,
yes, for mobile applications you access audio files that are part of the application bundle. In this case, the audio files are separate and are not stored in the LiveCode stack. On desktop, you can include audio files in your LiveCode stack. Have a look at "audioClip" in the LiveCode dictionary.
You can get the length of an audio file by searching for "duration" in the LiveCode dictionary.
You can find out more about the player object by selecting "Object->Player" in the left hand side of the LiveCode dictionary.
Kind Regards,
Hanson
Ian Stewart
Hi
In relation to the playing of audio files and the 'wait until the sound is done' code, is there a way of stopping further sound files from playing while an initial sound file is playing but not stopping other events from going ahead?
Regards
Ian S
Hanson Schmidt-Cornelius
Hi Stewart,
indeed you can. Have a look at the "wait" command in the dictionary. In particular, you may want to try using "wait until the sound is done with messages".
A small example may be something like the following. This allows you to hit the "yes" button several times to launch another dialog, while the sound is playing.
on mouseUp
play audioClip "mySound.aiff"
send "doThis" to me
wait until the sound is done with messages
answer "Sound is done!"
end mouseUp
command doThis
answer "doing something" with "yes" or "no"
if it is "yes" then
doThis
end if
end doThis
Kind Regards,
Hanson
Ian Stewart
Thanks Hanson, I'll try that.
Best
Ian
Ian Stewart
Hi again,
This was my last query:
"In relation to the playing of audio files and the 'wait until the sound is done' code, is there a way of stopping further sound files from playing while an initial sound file is playing but not stopping other events from going ahead?"
This was satisfactorily answered, thank you, but I have an additional question on this theme. Is there a way of stopping everything but a limited number of functions or even just one function while the sound file is playing? For instance, I might want to have a 'Quit' function that still works while everything else is overridden by the audio output.
Thanks
Ian
Hanson Schmidt-Cornelius
Hi Ian,
sorry for using your surname to address you last time.
There are several ways to implement this kind of functionality, and it depends very much on what functionality you want to control. I have added three suggestions here:
1. You may want to hid any buttons the user should not be using while a sound is playing. You can conveniently hide a number of buttons by adding them to a group and hiding the group. Have a look at dictionary for the "visible" property.
2. If you want to keep buttons visible you could set a flag when you start to play a sound and unset the flag once the sound has stopped playing. You would then test for the flag each time you execute a command that you want to disable at play time. A flag may be a global variable that has a "true" or "false" state.
3. If you are running other actions that are supposed to stop, you may call a "stopMyActions" command before you play a sound and call a "resumeMyActions" (command names randomly chosen) command after the sound has finished playing. You would add code to these start and stop commands that control the execution of any other actions that may be running.
Kind Regards,
Hanson
Ian Stewart
Hanson,
No problem. Thanks for your reply.
I'm not sure those suggestions fit exactly what I'm looking for.
Is the following possible though-
Under the assumption that the code ('Wait until the sound is done') works to stop everything else in the handler that would otherwise occur until the audio is over, is there supplementary code (maybe that goes in the same line?) that would stop everything as usual but that would allow just one function (or a limited range of functions) to work?
For example is there something like:
'Wait until the sound is done with quit'
(i.e., analogous to 'with messages')
- which would stop everything as per usual but allow a user to engage a user-generated 'quit' button or 'quit' function?
Regards
Ian
Hanson Schmidt-Cornelius
Hi Ian,
no, I am sorry, there is no short way to do this.
Try using a global flag that is set when the sound is playing. Something like:
global gSoundPlaying
on mouseUp
put true into gSoundPlaying
play audioClip "mySound.aiff"
wait until the sound is done with messages
put false into gSoundPlaying
end mouseUp
Then in the button that is to be blocked while the sound is playing do something like this:
global gSoundPlaying
on mouseUp
if gSoundPlaying then
answer "cannot do this at the moment - sound is playing" with "Okay"
else
answer "can do this - sound is not playing" with "Okay"
end if
end mouseUp
Any button that is not to be blocked does not require the if statement. So your "Quit" button could be implemented without any changes required.
Also remember that you should set the global flag before it is used.
Kind Regards,
Hanson
Jack
Also, just wanted to say I have spend just a few days with LiveCode and love it!
Question:
I have many audio files in my app and would like a better way to manage them in the script. In C we'd use something like the following:
#define dogbark /desktop/sounds/dogbark.wav
So I would just refer to dogbark and not the entire path. Is there a good way to help manage this in LiveCode? Thanks!
Hanson Schmidt-Cornelius
Hi Jack,
great you are liking LiveCode.
The closest you would get to a #define in C/C++ is the "constant" command. Have a look at that in the dictionary. You may also want to have a look at the "customProperties" property in the dictionary.
Kind Regards,
Hanson
Sam
Hi, I tried using the play audioclip but it didn't work, what i have is this:
case 0
put "You have won £0. Thank you for playing, better luck next time." into line 5 of field "Field"
play audioClip "C:/Users/Pincer/DesktopSounds/Ohhhh.wav" looping
wait 10 seconds
play stop
break
and more similar, can some1 help?
Hanson Schmidt-Cornelius
Hi Sam,
there could be a number of reasons why your audio clip is not playing. You do not provide enough information in your comment for me to narrow the exact reason down.
Here are some suggestions you may want to look into:
- This syntax should only work on desktop machines, not on mobile devices.
- Check that the sound file exists and the path is correct.
- Check that the sound file contains the appropriate data in the format you specify.
Kind Regards,
Hanson
Sam
Sorry I took so long to get back to you, I'm creating a lottery program in livecode for my advanced higher computing project, got it all fine and working apart from the sound. It gets 6 numbers from the user then generates 6 random numbers then compares the 2 lots of numbers. It then displays the numbers they chose, the six the computer genterated and how many matching numbers.
For the project I needed to fit file handleing in, I figured the easyest way would be sounds so tried to use the code above to have it play different sounds based on how many matching numbers they have. At the moment it is in a case statment as my previous post showed. But the code doesn't work.
The tips you had don't work. I am doing it on my laptop so should work, the file path is correct, I just cut it down in previous post as it goes through around 15 folders and they are in wav format which should work.
Hanson Schmidt-Cornelius
Hi Sam,
okay. We need to break this down a bit.
Try typing the following line into the LiveCode Message Box:
play audioClip "C:/Users/Pincer/DesktopSounds/Ohhhh.wav"
If it does not play the sound, then there may be something wrong with the audio settings, the data in the file or the path to the file. You can then test if one of the default windows files work. These are available on windows 7 machines somewhere around: C:/Windows/Media
The following line worked for me:
play audioClip "C:/Windows/Media/ding.wav"
If this does play the sound, then your program is probably not entering the case statement. Try adding some breakpoints and stepping through the application as it runs.
Kind Regards,
Hanson
Sam
Hi Hanson,
Thank you for your help. Tried playing the windows sounds and it worked. The problem is probably the sound files I am trying to use. Will get new sound.
Thanks again for all your help.
Sam
fbi
Hi everyone!
I'm learning to use Livecode and I find it great! I'm doing lots of tutorial in order to improve but I have a problem with the sounds.
Is there a way to set a soundtrack that plays automatically when the app runs, exactly like a musical background? Where can I find a help for the right script? Sorry if you've just answered to this question, so please redirect me to the post! I've been creating an app (translated in 3 languages) for FREE didactical use.
Cheers from Italy
Hanson Schmidt-Cornelius
Hi FBI,
you do not specify on what platform you would like to play the sound, so I will provide you with a small sample that works on desktop machines. The "play audioClip" starts playback and then returns control back to your program. Try something like this in a button:
on mouseUp
local tI
play audioClip "SOME AUDIO FILE"
repeat with tI = 1 to 1000
put tI
wait for 10 milliseconds
end repeat
end mouseUp
This also opens the message box and counts to 1000 in a total of 10 seconds, showing you that LiveCode is doing something else at the same time while the sound is being played.
If you are working on mobile platforms then have a look at the dictionary entry for "mobilePlaySoundOnChannel".
Kind Regards,
Hanson
Dave B.
I'm working on an android tablet app with buttons on the screen, each plays a sound. I can get it to compile stand alone for windows but not for android. I include the sound files in the Copy Files area of settings. The windows app works and runs fine. I need it to work for an android tablet. Any help appreciated.
Thank you for a wonderful program such as LiveCode!
Dave
Hanson Schmidt-Cornelius
Hi Dave,
from your comment alone it is very hard to say where you are going wrong. You can play sound on android.
I will point you in the direction of lessons that should cover all the information required to deploy to android. Go trough the lessons from top to bottom and decide what you have already covered and what is still outstanding. Hope that helps.
http://lessons.runrev.com/s/lessons/m/4069/l/27385
http://lessons.runrev.com/s/lessons/m/4069/l/27733
http://lessons.runrev.com/s/lessons/m/4069/l/48552
If you come across a command/function that starts with "iphone", then check in the dictionary if the equivalent "mobile" version is available for android.
Kind Regards,
Hanson
ridham
I'm making a game for android device. How do I import an audio file so it is compatible with android format. I want to use the import function instead of this way.
tariq
Hi,
New to Livecode. Working through the lessons and all is going well. However, I just can't seem to get the sounds to work.
I drag a button onto the card and script it as follows:
on mouseUp
play audioClip "C:/users/v90001435/desktop/audiolesson/cheers.wav"
end mouseUp
I'm working on my laptop (windows) and I've tried various different wav audio files and even tried to move the file to variuos different locations but nothing seems to be working.
I have even tried to import the sound file directly onto the stack and when I open up the application browser the audio files is showing up but it just does not want to play.
The audio files that I have tried work normally when I open them up directly from their folder in VLC media player.
Any suggestions?
Thanks in advance!
Tariq
Hanson Schmidt-Cornelius
Hi Ridham,
you can import audio clips into your application using:
"File -> Import as Control -> Audio File ..."
This places the audio clip on an "Audioclips" card.
You can then play the clip in the same way that you play the files refer to on the file system. You just have to take the names that are displayed to you in the Project Browser. For example if you have an audio file with the entry:
Audioclip: myaudio.aif
Then you can play it with the following line of code:
play audioClip "myaudio.aif"
Kind Regards,
Hanson
Hanson Schmidt-Cornelius
Hi Tariq,
that is a very good question.
I am only guessing here, but these are obvious things that are probably working as you tested them with VLC anyway:
- The audio settings, are they not set to mute.
- Did you try different audio file formats. Maybe there is something wrong with the particular .wav file you are using. In particular the codec used by LiveCode may be different to the one used with VLC.
Kind Regards,
Hanson
Larry
I imported a .wav file into my application. I'm using the Commerical 6.5.2
I entered this in a button:
play audioClip "mySound.wav"
I get nothing!
Please help. Do I need to install Quicktime? I had a previous app written with the community 6.1.1 and it played .wav files just fine, but now it will not.
Elanor Buchanan
Hi Larry
This should work ok, I just tested it in LiveCode 6.5.2 here. Did you import the wav file using File -> Import As Control -> Audio file? If you you should be able to use
play audioClip "mySound.wav"
or
play "mySound.wav"
You don't need Quicktime installed. You can check if the Audioclip has imported ok by looking for it in the Project Browser to checking in the Message box with
put there is an audioclip "mySound.wav"
If none of this helps please contact [email protected] letting us know what platform you are working from and perhaps including a sample stack, or even just the sound file for us to investigate.
Kind regards
Elanor
Holly Steele
Using the run (browse) tool, I am able to play the sound (by pressing a button) and stop the sound (by pressing a different button) in my stack.
The sound still plays in the iOS simulator, but it does NOT stop when I click on the stop button. Any ideas?
Play Button Script:
on mouseUp
play audioClip "/Users/.../LiveCode/SoundPractice/apollo11.WAV"
end mouseUp
Stop Button Script:
on mouseUp
play stop
end mouseUp
Elanor Buchanan
Hi Holly
The audioClip object is not supported on mobile devices, try using the play file command with a file path e.g.
play (specialFolderPath("engine) & "/applause.mp3")
There is a lesson specifically on playing sounds on iOS devices which you might find useful at
http://lessons.runrev.com/m/4069/l/12353-how-do-i-play-sounds-on-an-ios-device
We will also update the play command documentation to clarify this.
I hope that helps.
Kind regards
Elanor
Rafi
I trying to play an .mp3 file that I imported as control on a Nexus 7 but can't get the file to play. I used the special path function but that doesn't seem to help. Can you help?
Elanor Buchanan
Hi Rafi
Using import as control is not supported on mobile, as you mention using the special file path function is the correct way to play a sound file on mobile. There are a couple of lessons on this(which apply to iOS and Android) at
http://lessons.runrev.com/m/4069/l/12353-how-do-i-play-sounds-on-an-ios-device
http://lessons.runrev.com/m/847/l/58671-using-multi-channel-audio-on-mobile
If this doesn't help perhaps you could paste the line of code you are using here so we can take a look at it.
Kind regards
Elanor
Mario2Sonic
Hello Elanor,
I have recently attempted to play an audio piece using LiveCode and the audio only comes back as static.
Here is the line of code that I have:
if prize_result = "You Lose!" then play audioClip "Sad_Trombone.wav"
I have imported the sound file as you told another user, through file. I also checked using the message box if the sound file was there. It was. Beforehand, since the mp3 file also came out as static (changed mp3 to wav).
Thanks,
Mario2Sonic
Mario2Sonic
Hi Elanor,
As a follow up to my previous comment, I have downloaded the wav version of the audio file. It still gives me static.
Thanks again,
Mario2Sonic
Elanor Buchanan
Hi Mario
What version of LiveCode are you using and what OS are you working on? If you can let us know we'll look into this.
If audioclips are not working you could use a player object instead. Drag a player onto your stack, set the filename property to the sound file you want to play either in the Property Inspector or in code and then use the start command to start the player.
There is a separate lesson on using players at
http://lessons.runrev.com/m/4603/l/44283-using-players
Kind regards
Elanor
walid
hey is there a way to play to sounds at once?
Elanor Buchanan
Hi walid
On mobile you can use the mobilePlaySoundOnChannel command and play the two sounds on separate channels. On desktop you can use 2 separate players.
I hope that helps.
Elanor
trevix
On my LC 9.6.2 standalone I play .wav sounds using "play tSoundPath".
Everything works fine on development and iOS and Android hardware.
I recently tried the standalone in a poor performing Android 9 hardware and i noticed that most of the time the first part of the sounds got chopped off, like if the standalone was trying to catch up.
Is there a difference in performance when playing sounds from path or playing sound imported as controls?
If not, is there a way to preload a sound?
Elanor Buchanan
Hi Trevix
You have a couple of other options for playing sound files on Android.
The first is mobilePlaySoundOnChannel, and related commands. The second is to use a native player control created with mobileControlCreate.
I did a quick test on the Android device I have here and could not recreate the delay so if neither of these suggestions work this might need to be reported as a bug.
Elanor
Areo
Hi,
Is there a way to start playing a sound from a specific point? For example "play from the third second on" or "start at 5.33 second and end at 6.22 second point" of a file. Mac-Desktop. Thanks
Panos Merakos
Hello Areo,
On Mac you could use the player object. Just drag a player object from the Tools palette onto your card. Then, open the Property Inspector and set the filename. You can also check the "playSelection" property, and specify a startTime and an endTime. Note that startTime/endTime are NOT in seconds, but they are in (seconds * the timeScale of the player). So, in your example, you can make the player start at 5.33 second and end at 6.22 by doing this:
on mouseUp pButtonNumber
set the playSelection of player "myPlayer" to true
set the startTime of player "myPlayer" to 5.33 * the timescale of player "myPlayer"
set the endTime of player "myPlayer" to 6.22 * the timescale of player "myPlayer"
start player "myPlayer"
end mouseUp
Hope this helps.
Cheers,
Panos
Katherine
Hi,
I am a beginner, trying to add audio. I have done what was shown above and also tried adding as a control but whenever I press on the button all I hear is static. I have tried wav and mp3 and the result is the same. I am on a Mac using version 10 of live code.
Panos Merakos
Hello Katherine,
The command definitely does not support .mp3. RE .wav, I _think_ they are supported in this command only on Windows.
I would suggest you use a player object to play your .mp3 file. For example:
on mouseUp
if not (there is a player "myPlayer") then
new player "myPlayer"
end if
set the visible of player "myPlayer" to false
set the filename of player "myPlayer" to "/path/to/your/file.mp3"
start player "myPlayer"
end mouseUp
Hope this helps.
Kind regards,
Panos