Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Audio Mixer Slider doesn't work in Android

Discussion in 'Audio & Video' started by adrianfrancisco, Apr 12, 2022.

  1. adrianfrancisco

    adrianfrancisco

    Joined:
    Jul 29, 2021
    Posts:
    14
    So I found a question with the same problem as me, but sadly it wasn't answered. Anyone can help please?

    Basically the sliders that modify the Music and the Audio FXs work when I play in the editor mode in my computer, but not when building for android. I create the exposed parameters in the Audio Mixers that control the volume etc. As I said, it works when I hit play button, but not at all in my phone. This is the code:

    Code (CSharp):
    1.  using UnityEngine;
    2. using UnityEngine.Audio;
    3. using UnityEngine.UI;
    4. public class VolumeSliders : MonoBehaviour
    5. {
    6.      public AudioMixer mixer;
    7.      [SerializeField] private string audioMixerParameterName;//I create a PlayerPrefs float with the same Key
    8.      private Slider slider;
    9.      private void Awake()
    10.      {
    11.          slider = GetComponent<Slider>();
    12.      }
    13.      private void Start()
    14.      {
    15.          slider.value = PlayerPrefs.GetFloat(audioMixerParameterName, 1f);
    16.      }
    17.      public void SetVolume(Slider slider)
    18.      {
    19.          mixer.SetFloat(audioMixerParameterName, Mathf.Log10(slider.value) * 20);
    20.          PlayerPrefs.SetFloat(audioMixerParameterName, slider.value);
    21.          PlayerPrefs.Save();
    22.      }
    23. }
    Is there any fix or workaround this issue?
    Thank you
     
  2. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    You never call SetVolume in your script, but I assume you call it somewhere else, right? Can you make sure SetVolume is called even on android? What version of Unity are you working on? Are you on the latest LTS? Because if you are not and this is a bug, there is a chance it was already fixed.
     
  3. adrianfrancisco

    adrianfrancisco

    Joined:
    Jul 29, 2021
    Posts:
    14
    Thank you for your answer The_Island. My unity version is 2020.3.14f1, so maybe it has been fixed in newer versions.

    With respect to SetVolume, no I don't ever call it, could you please elaborate more on how to do it? I thought that just by calling:

    mixer.SetFloat(audioMixerParameterName, Mathf.Log10(slider.value) * 20);
    it would be enough to set the volume.
     
  4. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    For mixer.SetFloat to be set, you need to call the function SetVolume somewhere. You probably need to add a reference to your script on your slider so that when OnValueChanged happens, it calls SetVolume like this. On the same note, you have two variables named slider, which can be confusing. You should remove the Slider slider inside SetVolume if you are getting it in the Awake
    upload_2022-4-12_13-10-26.png
     
  5. adrianfrancisco

    adrianfrancisco

    Joined:
    Jul 29, 2021
    Posts:
    14
    Ohh yeah, sorry I misunderstood. I forgot SetVolume was my method, I thought it was a bult in Unity one. Yes of course I call that function exactly as you showed me. As I said, it works perfectly fine in Editor mode (I think this is important to notice). I tried what you suggested, updating to Unity 2021 LTS, and removing audio mixer as a parameter as I call it already in Awake, but nothing, the problem still persists, basically when I build it for android it stops working.

    Do you know any workaround or other way of implementing a volume slider with AudioMixer? Something else that I may try? Thanks!
     
  6. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    I don't see any issue with your code. The last thing I would do is check that SetVolume is called you get the value you expect from PlayerPrefs.GetFloat. Maybe for some reason, on android, PlayerPrefs.SetFloat is never called. When calling PlayerPrefs.GetFloat, if audioMixerParameterName doesn't exist, it will return the default value, potentially causing the issue. If the value return in your start is correct, then it looks like a bug. I would then ask you to open a bug report so we can investigate. https://unity3d.com/unity/qa/bug-reporting
     
    NachoMolina likes this.