- Home /
How to stop audio
I have vuforia ImageTarget app. When image is detect i need to play sound. I used AudioSource.PlayClipAtPoint(sound, transform.position); But if is not detecting i won't need to play audio. After i change the target image, still previous audio is playing. I'm using play() function inside this function OnTrackingFound(). It plays the sound. How can i remove the sound or stop inside this function private void OnTrackingLost()
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)
{
play( );
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);
// 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
}
Answer by Tomer-Barkan · Dec 24, 2013 at 02:08 PM
AudioSource.PlayClipAtPoint creates an object, attaches an audiosource, and plays it, all automatically for you. Unfortunately, it does not return a reference to the object it creates.
The best approach for you, is to create a GameObject with an audiosource in the hierarchy, add a reference to it in the script, and simply turn it on or off as you please:
public AudioSource audioSource; // set reference to object with audio source here in the inspector
public void Play() {
audioSource.transform.position = transform.position;
audioSource.PlayOneShot(sound);
}
private void OnTrackingLost() {
aduioSource.Stop();
// rest of your code here
}
[1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponents.html
Audio is not playing first..,@tbkn: Now audio is not playing. Your code is not working
You need to create a GameObject that is an audio source, and assign it to audioSource variable in the inspector. If you haven't already, take a look at this tutorial: http://www.youtube.com/watch?v=1B$$anonymous$$JFg$$anonymous$$68IU
tbkn: See the images of project. I have imageTarget, if image is detect i'm displaying cow 3d model. Here i assign AudioSource to cow. Then drag the cow audio clip to Audiosource. Then second image for ImageTarget inpector, here SoundPlay.cs script reference to ImageTarget. Then i used Cow object for AudioSource. But still it's not working.
SoundPlay script having 2 sounds. One is AudioClip varaible sound, another one is AudioSource. Is this cause problem?
Hmm.. It should work fine... Please add the following debug logs:
public void Play() {
audioSource.transform.position = transform.position;
audioSource.PlayOneShot(sound);
Debug.Log("playing: " + audioSource.isPlaying);
}
private void OnTrackingLost() {
Debug.Log("Tracking lost, stopping audio");
if (audioSource.isPlaying) {
aduioSource.Stop();
}
// rest of your code here
}
It could be that OnTrackingLost() is called when you don't expect it and causes the sound to stop immediately. You should see this in the debug log if this is the case.