Music and sound effects from audio mixer

hi, i have the following class responsible for music and sound effects ui button sprite swap and also their functionality.
however, now sprite changes correctly , but music and sound effects are not stopped and resumed correctly.
each time game starts they mess. any idea to fix that?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class SoundManager : MonoBehaviour
{
    public AudioMixer audioMixer;

    [SerializeField]
    private Image musicOnIcon;

    [SerializeField]
    private Image musicOffIcon;

    [SerializeField]
    private Image sfxOnIcon;

    [SerializeField]
    private Image sfxOffIcon;

    private bool musicIsMuted = false;
    private bool sfxIsMuted = false;

    // sprites save and load correctly.
    // music and sfx does not respond correctly base on sprite mode.
    // checking start method for music and sfx initializations.


    private void Start()
    {
        //if the key musicmuted value is false, then value 0 is set to that key.
        if (!PlayerPrefs.HasKey("musicmuted"))
        {
            PlayerPrefs.SetInt("musicmuted", 0);
            Load();
        }
        else
        {
            Load();
        }

        if (!PlayerPrefs.HasKey("sfxmuted"))
        {
            PlayerPrefs.SetInt("sfxmuted", 0);
            Load();
        }
        else
        {
            Load();
        }

        UpdateMusicIcon();
        UpdateSfxIcon();
        musicIsMuted = PlayerPrefs.GetInt("musicmuted") == 1;
        sfxIsMuted = PlayerPrefs.GetInt("sfxmuted") == 1;
    }

    public void OnMusicButtonPressed()
    {
        if (musicIsMuted == false)
        {
            musicIsMuted = true;
            audioMixer.SetFloat("music", -80f);
        }
        else
        {
            musicIsMuted = false;
            audioMixer.SetFloat("music", -0f);
        }

        UpdateMusicIcon();
        Save();
    }

    public void OnSFXButtonPressed()
    {
        if (sfxIsMuted == false)
        {
            sfxIsMuted = true;
            audioMixer.SetFloat("sfx", -80f);
        }
        else
        {
            sfxIsMuted = false;
            audioMixer.SetFloat("sfx", 0f);
        }

        UpdateSfxIcon();
        Save();
    }

    private void UpdateMusicIcon()
    {
        if (musicIsMuted == false)
        {
            musicOnIcon.enabled = false;
            musicOffIcon.enabled = true;
        }
        else
        {
            musicOnIcon.enabled = true;
            musicOffIcon.enabled = false;
        }
    }

    private void UpdateSfxIcon()
    {
        if (sfxIsMuted == false)
        {
            sfxOnIcon.enabled = false;
            sfxOffIcon.enabled = true;
        }
        else
        {
            sfxOnIcon.enabled = true;
            sfxOffIcon.enabled = false;
        }
    }

    private void Save()
    {
        PlayerPrefs.SetInt("musicmuted", musicIsMuted ? 1 : 0);
        PlayerPrefs.SetInt("sfxmuted", sfxIsMuted ? 1 : 0);
    }

    private void Load()
    {
        musicIsMuted = PlayerPrefs.GetInt("musicmuted") == 1;
        sfxIsMuted = PlayerPrefs.GetInt("sfxmuted") == 1;
    }
}

What does ‘mess’ and ‘not stopped correctly’ mean exactly? It is muted but doesn’t pause or is it not muted? What are those floats you’re setting on your AudioMixer? You may want to call Pause on your audio source if you want to stop the music (or Stop, depends).

1 Like

Hi and thanks for your help.

what i mean is imagine you have clicked on both music and sound effect button. so now you have muted the music and ui sound effect. ja?

however for me i need to click 2 times on each button to have music and sound effects muted
the other problem is when game is stopped and replayed again, music and sound effects mute is broken and music plays at the start.

how may i fix ui button click duplicate and cancellation of mute at the start please?

so What are those floats you’re setting on your AudioMixer?

in order to have control on each signal line , i.e music and sound effects , in the audio mixer there are two channel.and those floats are from -80 to 0 for mute and unmute the audio signal. each pair for music and sound effects.

hope it is clear to you what i need to be done with music settings.

Try something like this:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class SoundManager : MonoBehaviour
{
    [SerializeField]
    private AudioMixer audioMixer;
    [SerializeField]
    private Image musicOnIcon;
    [SerializeField]
    private Image musicOffIcon;
    [SerializeField]
    private Image sfxOnIcon;
    [SerializeField]
    private Image sfxOffIcon;

    private bool _musicIsMuted = false;
    private bool _sfxIsMuted = false;

    private const string MUSIC_PREFS_KEY = "musicmuted";
    private const string SFX_PREFS_KEY = "sfxmuted";
    private const string AUDIO_MIXER_MUSIC = "music";
    private const string AUDIO_MIXER_SFX = "sfx";
    private const int MUTED = 1;
    private const int NOT_MUTED = 0;
    private const float MUTED_VALUE = -80f;
    private const float NOT_MUTED_VALUE = 0f;

    private void Awake()
    {
        if (!PlayerPrefs.HasKey(MUSIC_PREFS_KEY)) PlayerPrefs.SetInt(MUSIC_PREFS_KEY, NOT_MUTED);
        if (!PlayerPrefs.HasKey(SFX_PREFS_KEY)) PlayerPrefs.SetInt(SFX_PREFS_KEY, NOT_MUTED);
        Load();

        UpdateMusicIcon();
        UpdateSfxIcon();
    }

    public void OnMusicButtonPressed()
    {
        _musicIsMuted = !_musicIsMuted;
        audioMixer.SetFloat(AUDIO_MIXER_MUSIC, _musicIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
        UpdateMusicIcon();
        Save();
    }

    public void OnSFXButtonPressed()
    {
        _sfxIsMuted = !_sfxIsMuted;
        audioMixer.SetFloat(AUDIO_MIXER_SFX, _sfxIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);

        UpdateSfxIcon();
        Save();
    }

    private void UpdateMusicIcon()
    {
        musicOnIcon.enabled = !_musicIsMuted;
        musicOffIcon.enabled = _musicIsMuted;
    }

    private void UpdateSfxIcon()
    {
        sfxOnIcon.enabled = !_sfxIsMuted;
        sfxOffIcon.enabled = _sfxIsMuted;
    }

    private void Save()
    {
        PlayerPrefs.SetInt(MUSIC_PREFS_KEY, _musicIsMuted ? MUTED : NOT_MUTED);
        PlayerPrefs.SetInt(SFX_PREFS_KEY, _sfxIsMuted ? MUTED : NOT_MUTED);
        PlayerPrefs.Save();
    }

    private void Load()
    {
        _musicIsMuted = PlayerPrefs.GetInt(MUSIC_PREFS_KEY) == MUTED;
        _sfxIsMuted = PlayerPrefs.GetInt(SFX_PREFS_KEY) == MUTED;
    }
}
1 Like

i have now used your class instead, however for some reason, as soon as game is ended and new session is started music and sound effects keep playing, but i did muted them the last time before leaving the game.

something could need to be fixed in awake method. or?

in some way i need to tell to the game the last time game is ended , music and sound effects were muted. and dont make them work now until user decides about them.

how to add this fix please?

otherwise, when game is ended when music and sound effects are not muted, in new session they keep playing as man expects.

IDK what is happening in your project, I tried this code now without audio, it is working. I added four images and all of them turn on/off as they should.

1 Like

images are working fine, however audio does not muted at the start. that is the only problem.
hope you find some solution please.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class SoundManager : MonoBehaviour
{
    [SerializeField]
    private AudioMixer audioMixer;
    [SerializeField]
    private Image musicOnIcon;
    [SerializeField]
    private Image musicOffIcon;
    [SerializeField]
    private Image sfxOnIcon;
    [SerializeField]
    private Image sfxOffIcon;

    private bool _musicIsMuted = false;
    private bool _sfxIsMuted = false;

    private const string MUSIC_PREFS_KEY = "musicmuted";
    private const string SFX_PREFS_KEY = "sfxmuted";
    private const string AUDIO_MIXER_MUSIC = "music";
    private const string AUDIO_MIXER_SFX = "sfx";
    private const int MUTED = 1;
    private const int NOT_MUTED = 0;
    private const float MUTED_VALUE = -80f;
    private const float NOT_MUTED_VALUE = 0f;

    private void Awake()
    {
        if (!PlayerPrefs.HasKey(MUSIC_PREFS_KEY)) PlayerPrefs.SetInt(MUSIC_PREFS_KEY, NOT_MUTED);
        if (!PlayerPrefs.HasKey(SFX_PREFS_KEY)) PlayerPrefs.SetInt(SFX_PREFS_KEY, NOT_MUTED);
        Load();

        UpdateMusic();
        UpdateSfx();
    }

    public void OnMusicButtonPressed()
    {
        _musicIsMuted = !_musicIsMuted;
        UpdateMusic();
        Save();
    }

    public void OnSFXButtonPressed()
    {
        _sfxIsMuted = !_sfxIsMuted;
        UpdateSfx();
        Save();
    }

    private void UpdateMusic()
    {
        musicOnIcon.enabled = !_musicIsMuted;
        musicOffIcon.enabled = _musicIsMuted;
        audioMixer.SetFloat(AUDIO_MIXER_MUSIC, _musicIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    }

    private void UpdateSfx()
    {
        sfxOnIcon.enabled = !_sfxIsMuted;
        sfxOffIcon.enabled = _sfxIsMuted;
        audioMixer.SetFloat(AUDIO_MIXER_SFX, _sfxIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    }

    private void Save()
    {
        PlayerPrefs.SetInt(MUSIC_PREFS_KEY, _musicIsMuted ? MUTED : NOT_MUTED);
        PlayerPrefs.SetInt(SFX_PREFS_KEY, _sfxIsMuted ? MUTED : NOT_MUTED);
        PlayerPrefs.Save();
    }

    private void Load()
    {
        _musicIsMuted = PlayerPrefs.GetInt(MUSIC_PREFS_KEY) == MUTED;
        _sfxIsMuted = PlayerPrefs.GetInt(SFX_PREFS_KEY) == MUTED;
    }
}
1 Like

still the same problem exist!

please help.

i hope still for some help. i am here since 2 days. please. why sounds play again when game starts?

music and sound effects dont get saved when quitting the game, why?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class AudioManager : MonoBehaviour
{
    private static AudioManager instance = null;

    public static AudioManager Instance
    {
        get { return instance; }
    }

    [SerializeField]
    private AudioMixer audioMixer;

    [SerializeField]
    private Image musicOnIcon;

    [SerializeField]
    private Image musicOffIcon;

    [SerializeField]
    private Image sfxOnIcon;

    [SerializeField]
    private Image sfxOffIcon;

    private bool _musicIsMuted = false;
    private bool _sfxIsMuted = false;

    private const string MUSIC_PREFS_KEY = "musicmuted";
    private const string SFX_PREFS_KEY = "sfxmuted";

    private const string AUDIO_MIXER_MUSIC = "music";
    private const string AUDIO_MIXER_SFX = "sfx";

    private const int MUTED = 1;
    private const int NOT_MUTED = 0;

    private const float MUTED_VALUE = -80f;
    private const float NOT_MUTED_VALUE = 0f;


    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);



        if (!PlayerPrefs.HasKey(MUSIC_PREFS_KEY))
        {
            PlayerPrefs.SetInt(MUSIC_PREFS_KEY, NOT_MUTED);
        }
       
        if (!PlayerPrefs.HasKey(SFX_PREFS_KEY))
        {
            PlayerPrefs.SetInt(SFX_PREFS_KEY, NOT_MUTED);
        }
           

        Load();

        UpdateMusic();
        UpdateSfx();
    }

    public void OnMusicButtonPressed()
    {
        _musicIsMuted = !_musicIsMuted;
        UpdateMusic();
        Save();
    }

    public void OnSFXButtonPressed()
    {
        _sfxIsMuted = !_sfxIsMuted;
        UpdateSfx();
        Save();
    }

    private void UpdateMusic()
    {
        musicOnIcon.enabled = !_musicIsMuted;
        musicOffIcon.enabled = _musicIsMuted;
        audioMixer.SetFloat(AUDIO_MIXER_MUSIC, _musicIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    }

    private void UpdateSfx()
    {
        sfxOnIcon.enabled = !_sfxIsMuted;
        sfxOffIcon.enabled = _sfxIsMuted;
        audioMixer.SetFloat(AUDIO_MIXER_SFX, _sfxIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    }

    private void Save()
    {
        PlayerPrefs.SetInt(MUSIC_PREFS_KEY, _musicIsMuted ? MUTED : NOT_MUTED);
        PlayerPrefs.SetInt(SFX_PREFS_KEY, _sfxIsMuted ? MUTED : NOT_MUTED);
        PlayerPrefs.Save();
    }

    private void Load()
    {
        _musicIsMuted = PlayerPrefs.GetInt(MUSIC_PREFS_KEY) == MUTED;
        _sfxIsMuted = PlayerPrefs.GetInt(SFX_PREFS_KEY) == MUTED;
    }
}