Log in or register to post comments

Audio file is not playing

December 23, 2013 - 6:47am #1
 I need to play sound & 3d object to be render while target tracking. I mada a .cs script file and added below code, then drag this script to ImageTarget. And imported the .mp3 file to asset folder. But it's not working.
 
code:
 
using UnityEngine;
using System.Collections;
 
public class sound : MonoBehaviour {
 
// Use this for initialization
void Start () {
 
GameObject go = GameObject.Find("bird");
go.animation.Play("Marudhaani");
 
}
 
// Update is called once per frame
void Update () {
 
}
}

 

AttachmentSize
Binary Data bytes

Audio file is not playing

April 21, 2017 - 12:31am #15

using UnityEngine;

using Vuforia;

public class SoundPlay: MonoBehaviour,ITrackableEventHandler

{

#region PRIVATE_MEMBER_VARIABLES

private TrackableBehaviour mTrackableBehaviour;

public AudioClip sound;

#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)

  {

   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<AudioSource>().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

}

Audio file is not playing

April 21, 2017 - 12:30am #14

Add this script to an image target ... finally work!!!

 

using UnityEngine;

using Vuforia;

public class SoundPlay: MonoBehaviour,ITrackableEventHandler

{

#region PRIVATE_MEMBER_VARIABLES

private TrackableBehaviour mTrackableBehaviour;

public AudioClip sound;

#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)

  {

   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<AudioSource>().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

}

 

Audio file is not playing

January 3, 2014 - 3:25am #13

Ok, great.

Audio file is not playing

January 3, 2014 - 3:12am #12

I tried with below code. It's working. 

 

public AudioClip clip;

Public AudioSource source;

TrackingFound:

source.transform.position=transform.position;

source.PlayOneShot(clip);

 

TrackingLost:

source.Stop();

Audio file is not playing

January 3, 2014 - 2:47am #11

I have written up a small tutorial for playing and stopping audio clips in response to image target detection / loss events:

https://developer.vuforia.com/forum/faq/unity-how-can-i-play-audio-when-targets-get-detected

Hope this helps.

Audio file is not playing

December 24, 2013 - 2:21am #10

If i point the camera to target the audio playing, then i move the camera the target lost. So, i don't need to play. Is it unity or vuforia? I'm confusing. Because target isn't based on vuforia?

Audio file is not playing

December 24, 2013 - 2:16am #9

Seriously ... this is outside the scope of these forums.

If you do not know how to use the Unity debugger then please refer to the Unity documentation at their web site.

 

thanks

N

Audio file is not playing

December 23, 2013 - 7:07pm #8

How can i use debugger?

Audio file is not playing

December 23, 2013 - 10:41am #7

 Why it's not losing?

You need to use the debugger, as really only you can answer your question.  You also need to look at the other thread wher you posted the identical code as I tried to answer there.

In general, you will have more luck in the forums if you carry out some of your own investigations this way and then highlight specific technical areas where we may be able to assist.  Simply saying "it is not working" provides little information for us to help.

Hope you can understand.

 

thanks

N

Audio file is not playing

December 23, 2013 - 10:38am #6

ok. This code is working. But after changing the target still previous audio sound is playing. Why it's not losing?

Audio file is not playing

December 23, 2013 - 10:36am #5

Can you please avoid double posting the same code and questions in multiple threads.

This causes confusion and delays.

 

thanks

N

Audio file is not playing

December 23, 2013 - 8:19am #4

I created one .cs script one added below code. Where to drag this script? To ImageTarget or 3d obj?

 

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>().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
}
 
 
 
 

Audio file is not playing

December 23, 2013 - 8:05am #3

I need to play sound while imagetracking. I have 3 target. When first target detect i need to display 3d object and play sound. I displayed 3d mdoels while image detect. Now i need to play sound. How can i do this? which code shall i change? If i point second image target then i need to play another sound.

Audio file is not playing

December 23, 2013 - 8:00am #2

This question seems to be about pure Unity programming, not Vuforia.

What about looking here :

http://unity3d.com/

?

 

 

Log in or register to post comments