Log in or register to post comments

Audio Stop On Tracking Lost

February 1, 2014 - 8:16am #1
I'm trying to 3 hours, but I could not.

I need to play a file audio on tracking Found, and to Stop the file audio on tracking lost

The file audio is named "suond"

This is my script :

/*==============================================================================
by fedbriz91
==============================================================================*/

using UnityEngine;
using System.Collections;

public class WordFadeTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private TrackableBehaviour mTrackableBehaviour;
	private bool isRendered = false;
	private GameObject Rb112;
	public AudioSource audioSource;
	public AudioClip soundEngine;          //soundEngine name of file audio in the Ispector
    #endregion // PRIVATE_MEMBER_VARIABLES

    #region UNTIY_MONOBEHAVIOUR_METHODS

	public void Play() {
		audioSource.transform.position = transform.position;
		audioSource.PlayOneShot(soundEngine);
		}
    
	void Start()
    {
		Rb112 = GameObject.Find ("Rb112");
			mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
		OnTrackingLost ();
    }

  void Update()
	{
				if (isRendered && Rb112.transform.position.x < 140) {
						
						Rb112.transform.position += Rb112.transform.forward * 50.0f * Time.deltaTime;
				
		}

	}
	#endregion // UNTIY_MONOBEHAVIOUR_METHODS

	#region // PUBLIC_METHODS
	/// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED)
		{
            
        
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


    private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
		audioSource.Play();
        
        // Enable rendering:
		foreach (Renderer component in rendererComponents){
        
            component.enabled = true;
        }

      

        Debug.Log ( "Trackable" + mTrackableBehaviour.TrackableName + "found");
			isRendered = true;
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
		audioSource.Stop();
 
        // Disable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = false;
        }


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

		isRendered = false;
    }

    #endregion // PRIVATE_METHODS
}

 

 
 the audio does not Play !
Where am I wrong?

 

 

 

Audio Stop On Tracking Lost

August 1, 2019 - 5:24pm #5

Hi I know this is a dated post, but I might be able to help.

I found the code below through another forum/post. It contains audioSource.Stop OnTrackingLost().

All You have to do is add an Audio Source under your Image Target. And add an Audio Clip into your Audio Source. Make sure "play on awake" is unticked/disabled.

Then inside your Image Target, add the Script component below. Drag in your Audio Source and Audio Clip under the script.

My script name is "PlayAudio", so edit the public class below to match whatever you've named your new script.

 

Should work as it did for me! Took me a three days to find the solution! Hope this helps anyone else out there!

ps: credit to whoever made the script, forgot which forum/post I got it from

 

 

using UnityEngine;

using Vuforia;

using System.Collections;

public class PlayAudio : MonoBehaviour, ITrackableEventHandler

{



    #region PRIVATE_MEMBER_VARIABLES

    private TrackableBehaviour mTrackableBehaviour;

    public AudioClip sound;

    public AudioSource audioSource;

    #endregion // PRIVATE_MEMBER_VARIABLES

 

    #region UNTIY_MONOBEHAVIOUR_METHODS

    // Use this for initialization

    void Start()

    {

        //audio.Stop();

        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)

        {

            OnTrackingFound();

        }

        else

        {

            OnTrackingLost();

        }

    }

    #endregion // PUBLIC_METHODS

    #region PRIVATE_METHODS



    private void OnTrackingFound()

    {

 

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

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

        // 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");

        Play();

    }



    private void OnTrackingLost()

    {

        Debug.Log("Tracking lost, stopping audio");

        audioSource.Stop;

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

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

        // Disable rendering:

        foreach (Renderer component in rendererComponents)

        {

            // audio.Stop();

            // AudioSource.Destroy(sound,transform.position);

            component.enabled = false;

        }

        // Disable colliders:

        foreach (Collider component in colliderComponents)

        {

            component.enabled = false;

        }

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



    }



    public void Play()

    {

        audioSource.transform.position = transform.position;

        audioSource.PlayOneShot(sound);

        Debug.Log("playing: " + audioSource.isPlaying);



    }

    #endregion // PRIVATE_METHODS

}

Audio Stop On Tracking Lost

July 19, 2019 - 9:01am #4

I have searched high and low. If i find a solution I will post it here 

Audio Stop On Tracking Lost

July 9, 2017 - 10:36am #3

Hi, I'm having the same problem. Did you ever get to solved the problem?

Audio Stop On Tracking Lost

February 2, 2014 - 1:24am #2
Log in or register to post comments