Search Unity

Audio Getting a Int/Float for the Volume of the sound

Discussion in 'Audio & Video' started by TrackLabs, Feb 20, 2019.

  1. TrackLabs

    TrackLabs

    Joined:
    Feb 18, 2019
    Posts:
    30
    I am trying to get a Integer (or probably Float) that represents the Volume of the Sound being playd.
    So I can play a Sound, and get a Int/Float in realtime from the volume. Just like you can show x,y,z positons or rotations or whatever in real time, I need something that does the same with Sound Volume.

    I already found this post Unity which does show a simple Script code, but it does not work however. I feel like the person that provided the Code forgot a few things. Or I am doing something wrong

    https://answers.unity.com/questions/544291/gameobject-reacts-to-audio-source.html

    Is there any (still working) way to achieve that?
     
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    I would need to know more about your setup, but here's a good start.
    https://docs.unity3d.com/ScriptReference/AudioSource.html

    Let's assume you have a game object called Fire and you have an audio source on that game object that is playing a fire sound. In the script on the fire gameobject you would have something like this (untested because I am not near my dev machine).

    AudioSource FireSound = GetComponent<AudioSource>();
    float FireVolume = FireSound.volume;

    Once again, this is untested but that should give you the float value of the current volume.

    To change it, you would ...

    FireSound.volume = 1.0f; //max volume, volume ranges from 0.0f (mute) to 1.0f (max)
     
  3. TrackLabs

    TrackLabs

    Joined:
    Feb 18, 2019
    Posts:
    30

    Thanks for the answer, I got it figured out earlier today. Based on the script provided there https://answers.unity.com/questions/1167177/how-do-i-get-the-current-volume-level-amplitude-of.html

    Had to do some changes to get it working exacly for my case, but if anyone needs to know how to get the current volume of a sound at a frame, that should do it
     
    wileyjerkins likes this.
  4. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Glad you got it working.