revUp - Updates and news for the Revolution community
Issue 126 | January 13th 2012 Contact the Editor | How to Contribute

Multi Channel Audio on iOS
When just one "beep" isn't enough, how can you get rich audio playing in your app?

by David Williams

Multi channel audio is a very useful feature available on iOS, which enables us to handle different pieces of audio separately by using separate 'channels'. This lesson will take you through the basics of using channel-based audio on iOS, and will show you how to use it to play multiple sounds simultaneously. All of the files used in this lesson can be downloaded here.

1. Getting set up

Screen_shot_2011-10-26_at_14.37.51_display

Firstly, we need to set up our stack so that we are ready to start playing audio. For this lesson, we will have four audio files that we use, named "background.wav", "noise1.wav", "noise2.wav", and "noise3.wav". We will want to put these audio files in the same folder as our stack, so the first thing we need to do is to create a new mainstack, and save it. Make sure you place it in it's own folder, by itself. Then, place the audio files in the same folder as the stack. You should now have a setup that resembles the above. As we are going to be running this app on a mobile device (or simulator), once this is done we also need to go into Standalone Application Settings, and under 'Copy Files', add in the four audio files that we have in the our application folder.

2. Playing an audio file

Screen_shot_2011-10-26_at_16.09.57_display

The first thing we are going to do is simply put a button on our stack, and script it to play one of our audio files. To do this, we will use the command 'iphonePlaySoundOnChannel', which takes three parameters - the filepath to the sound, the name of the channel, and the type of playback we want. The script of the button we will use to play sound 'noise1.wav' will be:

on mouseUp
put (specialFolderPath("engine") & "/noise1.wav") into tPath
put "noise1" into tChannelName
put "now" into tType
iphonePlaySoundOnChannel tPath, tChannelName, tType
end mouseUp

So, in the above example, we are using the specialFolderPath function to build the path to our sound - specialFolderPath("engine"), when used on a mobile device, returns the path to the folder containing our application and assosciated files. Adding "/noise1.wav" to the end of this gives the path to our noise1.wav audio file, which we included in the Copy Files in step 1. We use "noise1" as the channel name - this could be anything we want, but we use "noise1" for clarity here. The final parameter, the type, can be one of three things: "now", which plays the audio file now and interrupts any other currently playing audio files, "next" which will queue the sound and play it after the current sound on that channel is finished, and "looping", which will repeatedly play the sound file on that channel until we stop it.

So, if we now start up the app in the simulator, and click the button, we should hear our sound file "noise1.wav" being played.

3. Playing multiple audio files

Screen_shot_2011-10-26_at_18.20.32_display

Now that we have code to play an audio file properly, we can create several more buttons and reuse it (with slight alterations) in each button, to play multiple audio sounds. Create 3 other buttons on the stack, and use the same code as above in each - but change "noise1" to the name of the audio file we want to play in each new button (noise2, noise3, and background). For example,

on mouseUp
put (specialFolderPath("engine") & "/background.wav") into tPath
put "background" into tChannelName
put "now" into tType
iphonePlaySoundOnChannel tPath, tChannelName, tType
end mouseUp

When you run this in the simulator or on a device, you should be able to click each button to play a separate sound, without interrupting eachother.

4. Controlling audio playback

Screen_shot_2011-10-26_at_19.19.24_display

In our current setup, we are able to play the audio files, but we have no control over the playback. In some circumstances, we may want to be able to do things such as pause and resume the audio. Let's set up some controls to pause and resume our background.wav file, and alter the script of the button that plays 'background.wav' so that we can use it to both start the audio file from the beginning, or stop it completely.

We'll add two new buttons to pause and play, with the following scripts:

Pause:
on mouseUp
iphonePausePlayingOnChannel "background"
end mouseUp

Resume:
on mouseUp
iphoneResumePlayingOnChannel "background"
end mouseUp

These buttons use two very simple commands which will pause or resume the sound on a given channel. The script of the button that plays 'background.wav' becomes this:

on mouseUp
if (iphoneSoundOnChannel("background") is empty) or (iphoneSoundOnChannel("background") is "could not find channel") then
put (specialFolderPath("engine") & "/background.wav") into tPath
put "background" into tChannelName
put "now" into tType
iphonePlaySoundOnChannel tPath, tChannelName, tType
else
iphoneStopPlayingOnChannel "background"
end if
end mouseUp

The iphoneSoundOnChannel function is used to see if background.wav is already playing - if the function returns empty, no sound is playing on that channel. If the function returns 'could not find channel', the channel has not been created yet (it will automatically be created the first time we try to play a sound on it). If we find that it is already playing, we use the iphoneStopPlayingOnChannel command, and specify the channel "background" to tell it to stop playing the sound.

For a full list of channel related syntax, search for "channel" in the livecode dictionary.

Mark

About the Author

David Williams is a Software Developer for RunRev Ltd. His hobbies are coding, playing and listening to music, and gaming.

 

 

 

Main Menu

What's New

Sign up to the Game Academy