Vuforia: Playing Sound when image detected and stop sound when image lost. The simplest way (2021 Updated)

Prerequisite

  1. A sound, you can find some free sounds from here http://soundbible.com/

Lets Start!

Drag and Drop the sound to your ImageTarget

Playing sound on Image Detected

While your ImageTarget is selected, on the Inspector window drag and drop AudioSource to the On Target Found() Object slot

Click the function selector and select AudioSource > Play()

Stopping sound on image lost

While your ImageTarget is selected, on the Inspector window drag and drop AudioSource to the On Target Lost() Object slot

Click the function selector and select AudioSource > Stop()

Final check

Default Trackable Event Handler should now look like this

Done, lets test it!

When image is detected, you sound will play and when image is lost, the sound will stop.

I have multiple Image Targets and sound to implement. How?

Its okay you now know all you need to know to do this. All you have to do is repeat these steps from “Let’s Start” to the end for each of your Image Target.

That’s all, happy creating

Playing Sound on Marker Detected in XZIMG Augmented Vision

Prerequisite

I order to start this tutorial, you would need:

  1. You need to have finished the previous tutorial on Detecting Marker Detected and Lost
  2. You need a sound. You can find lots of it to download here

What type of sound format does Unity support?

It is best to go for mp3 sounds as sometimes in my experience the .wav have a tendency to not play properly.

Import Sound Into Unity

So you have downloaded the sound now lets import it into Unity. Drag and drop the sound into the Assets folder, in the Project window.

Using the downloaded sound

In the Hierarchy window, right click the Main Camera, under Audio, click Audio Source

An Audio Source will be created under the Main Camera. It will look something like this

Select the Audio Source, with the Audio Source selected, drag and drop the sound to the Audio Clip slot.

Next, uncheck Play On Awake.

Triggering the sound to play when marker is detected

Select Pivot1, look at the Inspector window, under Event Receiver, press the “+” at On Marker Found()

You will get something like this

Drag and Drop Audio Source to the Object Slot

Click the Function Dropdown, Go to Audio Source, and select Play()

Now lets Play it and test it out

The marker is detected and sound is now playing