In this tutorial you'll learn to add sound, and to make the sound play in response to an interaction from the person using your effect.
When you’ve got to grips with the basics of audio, take a look at the audio effect patches included in Spark AR Studio. They can be added to your project to distort audio clips or input from a device’s microphone.
Download the sample content to follow along. Open the unfinished project in the sample content folder to get started.
We've already added an animated 3D object to this project. We've also added an audio clip. It's listed in the Assets panel as boombox_loop.m4a:
Start by creating an audio playback controller. You can do this in the Assets panel:
It'll be listed in the Assets panel as audioPlaybackController0.
When an audio playback controller is selected in the Assets panel, you'll see its properties in the Inspector.
Add the audio file next to the option labeled Audio. To do this:
For sound to actually render in a scene, the audio playback controller has to be connected to an object called a speaker.
To add a speaker:
It’ll be listed in the Scene panel as speaker0:
Next apply the playback controller to the speaker:
You can also edit the speaker’s properties to adjust the volume of sound in the scene, using the slider next to Volume.
To add a looping sound adjust the properties of the playback controller:
You’ll now have a continuous looping sound in the scene!
You can use the Patch Editor to create effects with sound that include logic and interactivity.
Let's make this sound stop in response to a tap on the device screen.
Creating the patches you’ll need
In the Inspector there are arrows next to Play, Loop and Reset. Clicking these will create patches, representing that property.
Click the arrow next to Play to create a patch and open the Patch Editor.
Next create a Screen Tap patch. This is one of many interaction patches included in Spark AR Studio. It will detect when someone touches the screen of their device when using the effect.
To add this patch to your project:
Connecting the patches
You’ve now got 2 patches in the Patch Editor.
If we connect the output of the Screen Tap patch to the Play input port in the audio playback controller patch a Switch patch will be created automatically.
Joining these 3 patches together creates a switch that will turn the sound on or off each time the screen is tapped.
We can test this in the Simulator. Click the gear and select Simulate Touch, then click anywhere in the Simulator.