Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How do you mute a AudioMixerGroup from code?

Discussion in 'Audio & Video' started by jerotas, Mar 10, 2015.

  1. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    4,991
    I typed the following code expecting to see mute, solo and bypassEffects methods or properties, and I see...NONE of it from Intellisense. Where the heck is it hiding? How do you do that?

    Code (csharp):
    1.  
    2. public AudioMixerGroup grp; // assign from Inspector
    3.  
    4. grp. // where are the mute / solo API?
    5.  
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    You can either use mixer snapshots and transition to one of those snapshots, or you can expose any of the settings (like volume) for code, then use the exposed setting in your code (via AudioMixer.)
     
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    4,991
    Snapshots are overkill. I only want to change the mute status of a couple Groups, nothing more. How do we expose the mute button for a Group and set it from code?

    I don't understand why this needs to be done. I have no idea why they didn't just put the mute and solo switches into the normal API. It would be very useful.
     
    Last edited: Mar 10, 2015
    phobos2077 and Zelek like this.
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Why is a snapshot overkill? You can transition to it with a duration of zero, effectively muting all your desired groups at once. Also, it is nicer to configure the snapshot from the editor than having to hardwire the muting into your code.

    The mute button itself cannot be exposed to scripting, it seems. Instead, you just select any group, then in the inspector, right-click on the "Volume" label, and select "Expose 'Volume ...' to script." Then in your script, you can do this:

    mixer.SetFloat("volume", 0f);
     
  5. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    4,991
    The muting would be far more useful for what I'm trying to do. Since I want to have a separately mutable music volume, SFX volume and ambient volume, that means I'd need to create 2x2x2 = 8 snapshots to get all the mute combinations? Ludicrous.

    Snapshots are great for some things, but the mute control should still be exposed automatically from code. No solution to this?

    I don't necessarily want to set the volume...just mute it. I don't want to lose the volume level previously set.
     
    paulomuggler, Anisoropos and Zelek like this.
  6. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,016
    I've come to the same conclusion.
    I'm looking into the mixer for a procedural project (using Master Audio) and while the manual looks great you can't seem to access anything low level.

    How you you control any of the mixer FX from code, for example?
     
  7. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    FX params: right click, select expose parameter, name your param, enjoy. Only floats, only from the main thread.
     
  8. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,016
    Last edited: Mar 11, 2015
  9. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    No. Granted, the snapshots are mixer-wide, but you can always create sub-mixers. In your case, you could create separate mixers for music, sfx, and ambient, all of which only have the default "Master" group. Then, you can route those mixers into your main mixer. For the three mixers, you can set up snapshots to mute them separately.

    I agree. I don't think it is possible right now, though.

    I see. It wasn't clear to me you wanted to "keep" the previous volume.
     
    Zelek likes this.
  10. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    4,991
    Ok, thanks. Is there somewhere we can "ask for" this capability to be added?
     
  11. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I'd say the issue tracker should be a good place.
     
    phobos2077 and jerotas like this.
  12. FlyingHighUp

    FlyingHighUp

    Joined:
    Apr 23, 2012
    Posts:
    14
    Is there performance savings by using the mute vs. setting the volume to 0?
    This was my only motivation for trying to use it.
     
  13. Anjoom

    Anjoom

    Joined:
    May 15, 2017
    Posts:
    3
    Umm... Even exposing the volume parameter isnt working for me. I get an error CS1061 saying
    Type 'UnityEngine.Audii.AudioMixerGroup' doe not contain a definition for 'SetFloat' and no extention method 'SetFloat' of typr 'UnityEngine.Audio.AudioMixerGroup' could be found. Are you missing an assembly reference?
    Sorry for the noob question
     
  14. dylansunkel

    dylansunkel

    Joined:
    Jun 4, 2016
    Posts:
    1
    YourAudioMixerGroup.audioMixer.SetFloat("MyExposedParamName", 0f);
     
  15. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    52
  16. OSMET

    OSMET

    Joined:
    Jun 22, 2017
    Posts:
    2