play audio on an image target using vuforia in unity 3d

In real world to augment virtual objects augmented reality is used. In this tutorial you will learn to Play audio on an image target using vuforia in unity 3D. The video below will help you for better understanding of this blog.
For better understanding make sure to read the previous blogs i.e. DISPLAY 3D MODEL ON IMAGE TARGET USING VUFORIA IN UNITY3D ,DISPLAY IMAGE ON AN IMAGE TARGET OR MARKER USING VUFORIA and DISPLAY VIDEO ON AN IMAGE TARGET OR MARKER USING VUFORIA IN UNITY 3D.
Create a new unity project:
In augmented reality creating a new unity project requires some of the things to be done. For creating a new unity project you need to write project name and then click on create project.

Adding new image:
For adding a new image target you need to go to vuforia engine. After going to vuforia engine click on image and re position it. Now go to vuforia portal. For opening vuforia developer portal make sure that you are sign in. Click on develop and after that go to click on target manager. For adding the target click on single image. Now choose the image by clicking on the browse button, then add certain width and rename it. Then click on add.

Scene Setting:
Now we have to set scene for augmented reality. For that you have to delete main camera and add AR camera and image. As you can see this warning sign, go to build settings then go to player settings. Go to the bottom click on XR settings and check the vuforia augmented reality.

Downloading the database:
Now download that data base. Click on unity editor then click on download. When it is downloaded drag it to the unity timeline put it in assets. Make sure that all the items are selected and click on import. Go to image target and select your database. Now you can see your image has loaded.

Importing the audio clip:
Now as we want to play audio on an image target. Go to audio and then audio source. Let’s import it in unity first and drag this into the audio clip section.

Playing the audio:
We want to play the audio only when the image target is visible. So unchecked the play on awake. If you want to loop the audio means you want to play the audio again and again when it ends. So, check the loop checkbox. We want to play audio only when the target image is found and we want to stop the audio when the image target is lost.

Coding of track able event handler:
The code of this track able event handler file is totally same as to the default track able event handler file that is attached to our image target. We will open this file. We only have to do few minor changing in code. Press Ctrl A and then Ctrl C and paste it to the track able event handler file.
Now in the track able event handler file delete the default placed code and press Ctrl V to paste the default track able event handler code in this file. Coding of track able event handler:heck the name of the file which is track able event handler file and give the same name to the class.
Adding unity engine event:
So, basically what we have to change in this file is that we are going to add unity engine event. So, we will be using codes as:
using Unity Engine. Events;
Declaring public variables:
We need to declare two public variables onTrack and onLost.
public UnityEvent onTrack;
public UnityEvent onLost;


The event onTrack needs to be called when tracking is found. So, here we will write
onTrack.Invoke();
The second unity event onLost needs to be called when tracker is lost. So, here we will write
onLost.Invoke();
Play, stop and pause audio:
Save the file. Now attach this code to the image target and delete the default attached component. After that you will see the two events which are the public events that we have initialized onTrack and onLost. Click on the + icon in onTrack event. Here onTrack event drag the audio source. OnTrack we want to play the audio, audio source.play() and onLost repeat the same steps. Drag the audio source and go to audio source.pause() or audio source.stop().

Export Application :
From top menu click on file, then click on build settings. Check the platform is android if not switch it. And then click on add open scenes. Now click on player settings. Go to other settings. Change the package name according to your choice. Unchecked x86 and Android TV compatibility. Change the stripping level to low. Click on XR settings and make sure vuforia augmented reality is selected. And then you can insert the icon of your app. That’s all for settings now connect your android mobile with your computer and click on build and run. Now name it and save it.