0

In a music application in Unity, when you put a fadeout so that the sounds that are being created have no clicking when they stop, all the music that is in the background also enters into the fadeout. I would like to be able to do fadeout without interfering in the background music, but i don't know how. This is my code:

private AudioSource[] sound;

void OnTouchDown()
{

    if (instrument == "Piano")
    {
        FindObjectOfType<PianoAudioManager>().Play("PianoGmaj1G"); 
    }

void OnTouchUp()
{        
    sound = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
    foreach (AudioSource audioS in sound)
    {
        StartCoroutine(AudioFadeOut.FadeOut(audioS, 0.02f));
    }
}

How could I save the sound when I activate it so I can do the fadeout without affecting everything else? I have been stuck with this for days and I can not find how to do it. I would be very grateful for any help. Thank you

Edit. Yes, sorry, my PianoAudioManager code is:

public class PianoAudioManager : MonoBehaviour{

    public PianoSound[] PianoSounds;

    public static PianoAudioManager instance; 
    public AudioMixerGroup PianoMixer;

    void Awake() 
    {

        if (instance == null)
            instance = this;
        else
        {
            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject); 

        foreach(PianoSound s in PianoSounds)
        {
            s.source = gameObject.AddComponent<AudioSource>(); 
            s.source.outputAudioMixerGroup = PianoMixer;
            s.source.clip = s.clip;

            s.source.volume = s.volume;
            s.source.pitch = s.pitch;
        }
    }

    public void Play (string name)
    {
        PianoSound s = Array.Find(PianoSounds, sound => sound.name == name);  
        if (s == null)
        {
            Debug.LogWarning("Sound: " + name + " not found!");
            return;
        }
        s.source.Play();
    }
}
Mars
  • 2,505
  • 17
  • 26
Jose
  • 3
  • 1
  • 5
  • 1
    https://docs.unity3d.com/Manual/AudioMixer.html – Immersive May 17 '19 at 01:10
  • I dont understand what you want. Do you want to save a reference to an object on `OnTouchDown`, then start the fadeout for ONLY that object on `OnTouchUp`? – Mars May 17 '19 at 01:13
  • please add the code for `PianoAudioManager`. In general you shouldn't use `Find` over and over again. Store the results and reuse them later – derHugo May 17 '19 at 05:41
  • I edited the post with the PianoAudioManager code. Thank you very much to all of you who are trying to help me. – Jose May 17 '19 at 09:20

1 Answers1

0

Right now, you are fading out every single AudioSource in your scene, but I think you only want to fade out the audiosource that is playing your piano noise. If that's correct, then you need to remember which audiosource is playing the sound, and fade out out just that one!

Add/change this in PianoAudioManager:

public List<PianoSound> soundsToFadeOut = new List<PianoSound>();
public void Play (string name)
{
    PianoSound s = Array.Find(PianoSounds, sound => sound.name == name);  
    if (s == null)
    {
        Debug.LogWarning("Sound: " + name + " not found!");
        return;
    }
    s.source.Play();
soundsToFadeOut.Add(s);
}

And change your other functions:

void OnTouchDown()
{

    if (instrument == "Piano")
    {
        PianoAudioManager playable = FindObjectOfType<PianoAudioManager>();
        playable.Play("PianoGmaj1G"); 
    }

void OnTouchUp()
{        
    PianoAudioManager playable = FindObjectOfType<PianoAudioManager>();
    foreach (PianoSound s in playable.soundsToFadeout)
    {
        StartCoroutine(AudioFadeOut.FadeOut(s.source, 0.02f));
    }
    playable.soundsToFadeout.Clear();
}

Explanation:
You want to keep track of the sounds that are actually playing, and only fade out those songs!

Mars
  • 2,505
  • 17
  • 26
  • Yes, I'm looking for that, but it does not work that way either. I added the PianoAudioManager code to facilitate the help. I'm new to unity c # and maybe I'm asking stupid questions, but thank you very much for the help. – Jose May 17 '19 at 17:36
  • @Jose A little piece of advice: Check out `static` members! Instead of FindObjectOfType, it would be better if you could use `PianoAudioManager.Instance.xxx` – Mars May 20 '19 at 00:41