Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by karthees · Dec 24, 2013 at 10:46 AM · audiovuforiastopaudioplay

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
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karthees · Dec 24, 2013 at 05:34 PM 0
Share

Audio is not playing first..,@tbkn: Now audio is not playing. Your code is not working

avatar image Tomer-Barkan · Dec 25, 2013 at 08:09 AM 0
Share

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

avatar image karthees · Dec 25, 2013 at 10:25 AM 0
Share

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.alt text

alt text

gameobject.png (170.3 kB)
assignscript.png (186.5 kB)
avatar image karthees · Dec 25, 2013 at 10:31 AM 0
Share

SoundPlay script having 2 sounds. One is AudioClip varaible sound, another one is AudioSource. Is this cause problem?

avatar image Tomer-Barkan · Dec 25, 2013 at 11:04 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Play local Android/IOS audio files in unity game. 0 Answers

Audio on collision is not playing 1 Answer

Audio not playing 1 Answer

How can I play audio clips depending on the players movement 0 Answers

Play audio in editor with a start time. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges