Log in or register to post comments

Why audio is not stopping after tracking loss

December 23, 2013 - 10:15am #1

I need to play sound while image is detect. The audio is playing, but when i move to another target, the 2 audio files are mixing. Previous target audio is not stopping.

 

using UnityEngine;
using System.Collections;
 
public class SoundPlay : MonoBehaviour,ITrackableEventHandler {
 
 
#region PRIVATE_MEMBER_VARIABLES
 
private TrackableBehaviour mTrackableBehaviour;
public AudioClip sound;
#endregion // PRIVATE_MEMBER_VARIABLES
 
 
 
#region UNTIY_MONOBEHAVIOUR_METHODS
 
// Use this for initialization
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)
    {
        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");
   // GetComponentInChildren<Marudhaani.mp3>().Play();
 
Play();
 
}
 
 
private void OnTrackingLost()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
 
    // 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");
}
 
 
 public void Play(){
    AudioSource.PlayClipAtPoint(sound, transform.position);
}
#endregion // PRIVATE_METHODS
}
 
 
 
 

Why audio is not stopping after tracking loss

December 25, 2013 - 7:39am #8

The code is working. The best approach is, create a GameObject with an audiosource in the hierarchy, add a reference to it in the script, and simply turn it on or off. Below code is working. 

 

using UnityEngine;

using System.Collections;
 
public class SoundPlay : 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
}
 
 
 
 

Why audio is not stopping after tracking loss

December 25, 2013 - 6:26am #7

Yes, you need to have a script implementing the ITrackableEventHandler for each Image Target, indeed; this technique is shown in the samples as well.

 

Why audio is not stopping after tracking loss

December 24, 2013 - 9:48am #6

It's not wokring. I used below code. If i point one target it plays audio, but while i switch to another target prevoius audio is still playing, it won't stoping. I'm using one script file for all ImageTarget. Is i need to use separate script for each ImageTarget?

 

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()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
 
    // 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");
 
AudioSource audio = GetComponentInChildren<AudioSource>();
 
if (audio.isPlaying){
audio.Stop();
}
}
 
 
public void Play() {
 
AudioSource.PlayClipAtPoint(sound, transform.position);
}
 
 
#endregion // PRIVATE_METHODS
}
 

Why audio is not stopping after tracking loss

December 24, 2013 - 7:08am #5

I created script called in SoundPlay. And write code AudioSource.PlayAtOnce then drag this script to each ImageTarget. Then added audio clip for each script. I added audio file like this only.

Why audio is not stopping after tracking loss

December 24, 2013 - 7:03am #4

How do you retrieve the audio component that yu use in OnTrackingLost() ?

One thing you may try is to get the audio component attached to your game object with code like:

  AudioSource audio = GetComponentInChildren<AudioSource>();

and then 

   if (audio) audio.Stop();

See also:

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.Stop.html

 

Why audio is not stopping after tracking loss

December 24, 2013 - 6:49am #3

I used play code for OnTrackingLost(); but it's not working.

 

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");
   // GetComponentInChildren<Marudhaani.mp3>().Play();
 
AudioSource.PlayClipAtPoint(sound, transform.position);
 
 
}
 
 
private void OnTrackingLost()
{
    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");
 
 
audio.Stop();
}

Why audio is not stopping after tracking loss

December 23, 2013 - 10:34am #2

Why don't you try debugging it in play mode, to get to the root of the problem?

 

Previous target audio is not stopping.

I cannot see in your code where you are stopping this in OnTrackingLost()

 

N

Log in or register to post comments