Log in or register to post comments

Apply audio to virtualbutton

December 27, 2013 - 7:03am #1

I run the sample app. It's working. I need to play audio while cliking green button. I added Audiosource to greenbutton and wrote code. But it's not working. See the attached images/

public class VirtualButtonEventHandler : MonoBehaviour,
                                         IVirtualButtonEventHandler
{
public AudioClip sound;
 
public AudioSource audiosource;
 
 
 
 
    #region PUBLIC_MEMBER_VARIABLES
 
    /// <summary>
    /// The materials that will be set for the teapot model
    /// </summary>
    public Material[] m_TeapotMaterials;
 
    #endregion // PUBLIC_MEMBER_VARIABLES
 
 
 
    #region PRIVATE_MEMBER_VARIABLES
    
    private GameObject mTeapot;
    private List<Material> mActiveMaterials;
 
    #endregion // PRIVATE_MEMBER_VARIABLES
 
 
 
    #region UNITY_MONOBEHAVIOUR_METHODS
 
    void Start()
    {
        // Register with the virtual buttons TrackableBehaviour
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
        for (int i = 0; i < vbs.Length; ++i)
        {
            vbs[i].RegisterEventHandler(this);
        }
 
        // Get handle to the teapot object
        mTeapot = transform.FindChild("teapot").gameObject;
 
        // The list of active materials
        mActiveMaterials = new List<Material>();
    }
 
    #endregion // UNITY_MONOBEHAVIOUR_METHODS
 
 
 
    #region PUBLIC_METHODS
    
    /// <summary>
    /// Called when the virtual button has just been pressed:
    /// </summary>
    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
        Debug.Log("OnButtonPressed");
 
        if (!IsValid())
        {
            return;
        }
 
        // Add the material corresponding to this virtual button
        // to the active material list:
        switch (vb.VirtualButtonName)
        {
            case "red":
                mActiveMaterials.Add(m_TeapotMaterials[0]);
                break;
 
            case "blue":
                mActiveMaterials.Add(m_TeapotMaterials[1]);
                break;
 
            case "yellow":
                mActiveMaterials.Add(m_TeapotMaterials[2]);
                break;
 
            case "green":
 
 
                mActiveMaterials.Add(m_TeapotMaterials[3]);
 
audiosource.transform.position = transform.position;
audiosource.PlayOneShot(sound);
 
 
                break;
        }
 
        // Apply the new material:
        if (mActiveMaterials.Count > 0)
            mTeapot.renderer.material = mActiveMaterials[mActiveMaterials.Count - 1];
    }
 
AttachmentSize
Image icon Green_Audiosource.PNG214.69 KB
Image icon ImageTarget.PNG202.45 KB

Apply audio to virtualbutton

December 30, 2013 - 1:41am #4

I used same code for my another application. It's working fine. But for virtual button it's not working

Welcome to the joy of programming - context is everything.

 

If i hide the code for stop the audio the Audioclip is playing, but when i write the code for audioclip play even the audio is not playing. Why? 

Not quite sure how you expect anyone else to help you with this.  You need to think through this yourself.

 

I don't have play mode

Why not?  If you are using the Trial version this should be available, provided you have a webcam.

 

But where to find the log

Click where it says "Console"

 

N

Apply audio to virtualbutton

December 27, 2013 - 11:50pm #3

I used same code for my another application. It's working fine. But for virtual button it's not working. If i hide the code for stop the audio the Audioclip is playing, but when i write the code for audioclip play even the audio is not playing. Why? I'm using trail version. I don't have play mode. I tried debug.log with below code. But where to find the log. I tried with unity forum. But i didn't get answer.

 

public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
         Debug.Log("OnButtonPressed for " + vb.VirtualButtonName );
 
        if (!IsValid())
        {
            return;
        }
 
        // Add the material corresponding to this virtual button
        // to the active material list:
        switch (vb.VirtualButtonName)
        {
            case "red":
           
 
audiosource2.transform.position = transform.position;
audiosource2.PlayOneShot(sound2);
 
                break;
 
            case "blue":
       
 
audiosource1.transform.position = transform.position;
audiosource1.PlayOneShot(sound1);
 
                break;
 
            case "yellow":
            
 
audiosource3.transform.position = transform.position;
audiosource3.PlayOneShot(sound3);
 
 
                break;
 
            case "green":
 
audiosource.transform.position = transform.position;
audiosource.PlayOneShot(sound);
                break;
        }

Apply audio to virtualbutton

December 27, 2013 - 9:55am #2

Have you tried debugging it in Play Mode?

Why do you think it is not working?

What else have you tried?

How have you tried to narrow down the problem?

Have you tried running the code that you have written in a simple test app?

 

N

Log in or register to post comments