Log in or register to post comments

Adding Audio File into Target

December 8, 2012 - 11:51pm #1

Hi,

I would like too add an audio file (mp3) into the target. But it doesn't work even I was tested for a long time.

I tried add the script: audio.play into the TrackeEventHandler.cs

Also, I tried adding the Audio Source (Component -> Audio) into the object.

But this method also not work.

Anyone can help me and give me some suggestion.

Thanks and seeking for reply.

Oscar

Adding Audio File into Target

April 3, 2017 - 12:38pm #16

Hi 

I had that problem and i solved it . you must first scale your object in your scene as the real size in world.

For example if you have a bottle on a table your distance from the target must be maximum 2 meter .

Audio source also must be attached to the target object too and

the camera position must be far enough that the sound not be able to heard when

you are ready to build the project.

Do not forget to check on "play on awake " in Audio source component.

Good luck.

 

Adding Audio File into Target

October 18, 2015 - 7:46pm #15

Hello

 

So I have tried implementing the code previously posted in this thread and it doesn't seem to work and it actually seems to break the DefaultTrackableEventHandler. My models will sort of stick in one corner of the screen even when the tracker is not visible. But that is only one part of the problem, which is no sound playing when the trackers are visible. I've seen some videos of people refering to a "customTrackableEventHandler" component but I can't find it in the 5-0-6 Vuforia package. There also doesn't seem to be the ITrackableEventHandler component in the package.

I have also tried doing what was mentionned in this tutorial, but with no luck : https://developer.vuforia.com/forum/faq/unity-how-can-i-play-audio-when-targets-get-detected

I'm using Unity 5.2.1. 

Anyways, here is the code on the DefaultTrackableEvenHandler in the ImageTarget prefab:

 

/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
==============================================================================*/
 
using UnityEngine;
 
namespace Vuforia
{
    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class DefaultTrackableEventHandler : MonoBehaviour,
                                                ITrackableEventHandler
    {
        #region PRIVATE_MEMBER_VARIABLES
 
        private TrackableBehaviour mTrackableBehaviour;
    
        #endregion // PRIVATE_MEMBER_VARIABLES
 
 
 
        #region UNTIY_MONOBEHAVIOUR_METHODS
    
        void Start()
        {
            mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
            {
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
        }
 
        #endregion // UNTIY_MONOBEHAVIOUR_METHODS
 
 
 
        #region PUBLIC_METHODS
 
        /// <summary>
        /// Implementation of the ITrackableEventHandler function called when the
        /// tracking state changes.
        /// </summary>
        public void OnTrackableStateChanged(
                                        TrackableBehaviour.Status previousStatus,
                                        TrackableBehaviour.Status newStatus)
        {
            if (newStatus == TrackableBehaviour.Status.DETECTED ||
                newStatus == TrackableBehaviour.Status.TRACKED ||
                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                OnTrackingFound();
            }
            else
            {
                OnTrackingLost();
            }
        }
 
        #endregion // PUBLIC_METHODS
 
 
 
        #region PRIVATE_METHODS
 
 
        private void OnTrackingFound()
        {
            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
 
            // *** Additional Audio code 
            foreach (Transform child in transform)
            {
                child.GetComponent<AudioSource>().Play();
            }
 
            // Enable rendering:
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = true;
            }
 
            // Enable colliders:
            foreach (Collider component in colliderComponents)
            {
                component.enabled = true;
            }
 
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
        }
 
 
        private void OnTrackingLost()
        {
            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
 
            // *** Additional Audio code
            foreach (Transform child in transform)
            {
                child.GetComponent<AudioSource>().Stop();
            }
 
            // Disable rendering:
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = false;
            }
 
            // Disable colliders:
            foreach (Collider component in colliderComponents)
            {
                component.enabled = false;
            }
 
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
        }
 
        #endregion // PRIVATE_METHODS
    }
}
 

Adding Audio File into Target

October 8, 2014 - 11:02am #14

Hi, 

 

I'm really not much of a coder so i't really hard for me to get this going.

 

1 I have an FBX animation from cinema. improted successfully.

2 i have a sound file with the same lenght as the animation.

 

how i want it to behave

 

start animation and sound simultaniously on marker detection.

play both in loopmode as long as marker is tracked.

stop both on marker lost.

start both again from begining on marker detection and so on...

 

i'm sure it was covered here but somehow not in a way so that i got it 100%

 

please help

Adding Audio File into Target

September 10, 2013 - 3:05am #13

Glad the script worked for you and a thanks for sharing your update :)

N

Adding Audio File into Target

September 9, 2013 - 9:52pm #12

Hi,

I found the solution. You can add an audio source to the AR camera for playing a sound automatically. Since AR camera is having audio listener, it will play the sound. This is used for playing the background sound. For playing the other sound, please follow the code that is explained in this thread.

Also, if we need to play more than one sound for the tracked object, then follow the steps detailed in the thread: http://answers.unity3d.com/questions/175995/can-i-play-multiple-audiosources-from-one-gameobje.html

Thanks,

BP

 

Adding Audio File into Target

September 9, 2013 - 9:04pm #11

Hi N,

Thanks for the script. It worked fantastically for me. Is it possible to play a default sound automatically in the background and play a different one when the target is detected?

Thanks,

BP

Adding Audio File into Target

July 2, 2013 - 8:52am #10

You'd think this would be a simple thing?

Well, it is, but you need to write some script, very much like I described here if you have an animation attached, but instead with an audio source in this case attached to the augmentation (teapot)

https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/how-check-if-child-image-target-showed#comment-2028342

However, you also need to understand that as the tracking target is lost, it needs to stop playing the audio.  Furthermore, you need to uncheck "Play On Awake".

So instead the code you need is as below.

HTH

N

 

 

    private void OnTrackingFound()

    {

        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);

        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

 

        // *** Additional Audio code 

        foreach (Transform child in transform)

        {

            child.audio.Play();

        }

 

        // Enable rendering:

        foreach (Renderer component in rendererComponents)

        {

            component.enabled = true;

        }

 

        // Enable colliders:

        foreach (Collider component in colliderComponents)

        {

            component.enabled = true;

        }

 

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

    }

 

 

    private void OnTrackingLost()

    {

        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);

        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

 

        // *** Additional Audio code

        foreach (Transform child in transform)

        {

            child.audio.Stop();

        }

 

        // Disable rendering:

        foreach (Renderer component in rendererComponents)

        {

            component.enabled = false;

        }

 

        // Disable colliders:

        foreach (Collider component in colliderComponents)

        {

            component.enabled = false;

        }

 

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");

    }

 

 

 

 

Adding Audio File into Target

July 2, 2013 - 6:08am #9

I'm with you Oscar. I have an object parented to the ImageTarget. I've placed an audio source directly on the 3d object, as well as in a game object parented to it, and with play on awake on or off, all that happens is the audio starts before the tracker is found putting it out of sync. You'd think this would be a simple thing? Can no-one answer this? 

Adding Audio File into Target

June 12, 2013 - 2:28am #8

I think Vince highlighted this below:

You can always listen for the OnTrackingFound() event and call a method that plays the audio file and inversely pauses or stops the audio using audio.Stop(), etc when OnTrackingLost is called.

 

Your best bet is to check the Unity documentation.

N

Adding Audio File into Target

June 12, 2013 - 1:03am #7

hi nalin...

what code to stop it playing in OnTrackingLost() ??

Child?

December 18, 2012 - 1:31pm #6

Hey oscarlochihang,

Is your teapot a child of the image target?  If so, it will only appear when OnTrackingFound() is called and if you have "Play on Awake" set to true, it should play as soon as tracking is found.  If not, simply drag and drop the Teapot game object onto the ImageTarget prefab in your Heirarchy view to parent the Teapot to the ImageTarget.  Any children of an ImageTarget will be hidden/disabled until OnTrackingFound() is called, in which it will iterate through all the children of the target and activate them which should play your audio file.

You can always listen for the OnTrackingFound() event and call a method that plays the audio file and inversely pauses or stops the audio using audio.Stop(), etc when OnTrackingLost is called.

Hi Nalin, I have tried your

December 18, 2012 - 8:28am #5

Hi Nalin,

I have tried your suggestion. But the audio file play when the application starts instead of the teapot was tracked.

Am I did something wrong in here:

1) Add audio listener into AR Camera
2) Click Add component into the teapot and choose the audio file

Oscar

Hi Oscar, I never said to

December 16, 2012 - 10:15am #4

Hi Oscar,

I never said to write any script ;)

If you add the audio source as a component to the teapot and click "Play On Awake" then the sound should play when the teapot is enabled in the default tracker.  This means you should not require any script change.

Hope this works.

Nalin

Hi N, I have followed your

December 16, 2012 - 6:35am #3

Hi N,

I have followed your suggestion. But the audio start playing when the apps was executed instead of the target was tracked.

I add the following script into the private void OnTrackingFound(), 

audio.Play();

As I am new in Unity, could you please give me more hits or instruction.

Thanks so much.

Oscar

Adding Audio File into Target

December 10, 2012 - 7:10am #2

Hi Oscar,

This is pretty easy to do assuming that your ARCamera has an AudioListener component which it should do by default.

Taking image targets as an example all you need to do is add an Audio Source Component to the teapot inside it, and then set it's audio clip to be any wav/mp3 file that you have dragged into your project hierarchy.

Now when you point at your image target when the OnTrackingFound() method is called it will enable the augmentation and the audio source you have supplied.  In this simple example the audio just keeps playing until it is finished, so you may need to add some code to stop it playing in OnTrackingLost() if that is what you want.

HTH

N

Log in or register to post comments