How to route sound from PlayClipAtPoint() to a specific audio mixer group?

The only other answer I’ve found to a similar problem is to add the mixer as a public variable. But this can’t be done when using PlayClipAtPoint().

I’ve tried to write my own public static method that does what PlayClipAtPoint does, but I can’t change the audio mixer group in the script after adding the audiosource component.

Any ideas on how to achieve this?
Thanks!

It’s old question but for anyone still wondering, static method

PlayClipAtPoint

looks like this:

public static void PlayClipAtPoint
(AudioClip clip, Vector3 position, [DefaultValue("1.0F")] float volume)
    {
      GameObject gameObject = new GameObject("One shot audio");
      gameObject.transform.position = position;
      AudioSource audioSource = (AudioSource) gameObject.AddComponent(typeof (AudioSource));
      audioSource.clip = clip;
      audioSource.spatialBlend = 1f;
      audioSource.volume = volume;
      audioSource.Play();
      Object.Destroy((Object) gameObject, clip.length * 
((double) Time.timeScale < 0.009999999776482582 ? 0.01f : Time.timeScale));
    }

And after some modifications I’ve got this in result:
`


public static void PlayClipAtPoint
(AudioClip clip, Vector3 position, float volume = 1.0f, AudioMixerGroup group = null)
        {
            if (clip == null) return;
            GameObject gameObject = new GameObject("One shot audio");
            gameObject.transform.position = position;
            AudioSource audioSource = (AudioSource) gameObject.AddComponent(typeof (AudioSource));
            if(group != null)
                audioSource.outputAudioMixerGroup = group;
            audioSource.clip = clip;
            audioSource.spatialBlend = 1f;
            audioSource.volume = volume;
            audioSource.Play();
            Object.Destroy(gameObject, clip.length * 
(Time.timeScale < 0.009999999776482582 ? 0.01f : Time.timeScale));
        }

`

Hi,

I don’t know why you can’t change the outputAudioMixerGroup after adding the audio source. Also, PlayClipAtPoint() also uses default values for all AudioSource parameters for the temporary objects it makes, so if you want some specific behaviour, PlayClipAtPoint() is not very useful.

But with your replacement method you can do this:

  • Create a set of prefabs of gameobjects that have an AudioSource component

  • In the prefabs you can set different mixer groups and other AudioSource settings

  • In you replacement method instantiate one of these prefabs as your temporary audio source depending on situation. Something like:

      GameObject audioObject;
      if (type == AudioType.Announcer)
      	audioObject = Instantiate(announcerAudioPrefab);
      else
      	audioObject = Instantiate(worldAudioPrefab);
    
      audioObject.transform.position = position;
      AudioSource tAudio = audioObject.GetComponent<AudioSource>();
      tAudio.clip = clip;
      tAudio.Play();
    

Late as well, but I also have a different workaround which I found to be very simple. The 3rd parameter of PlayClipAtPoint() controls the volume, and you can get the volume from the Audio Mixer.
Attach an audio source to the object calling PlayClipAtPoint(), link the mixer to the output, and add the following code in. Tailor it to your specific case.

private AudioMixerGroup audioMixerGroup;

void Start()
{
    audioMixerGroup = GetComponent<AudioSource>().outputAudioMixerGroup;
}

private void PlaySound()
{
    bool value = audioMixerGroup.audioMixer.GetFloat("Sound", out float volume);
    volume = Mathf.Pow(10f, volume / 20); //converted from Log10
    AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position, volume);
},Late as well, but I have a different workaround which I found to be very simple. The 3rd parameter of PlayClipAtPoint() controls the volume, and you can get the volume from the Audio Mixer.

Just attach an audio source to the object calling PlayClipAtPoint(), link the mixer to the output, and add the following code in. Tailor it to your specific case.

private AudioMixerGroup audioMixerGroup;

void Start()
{
    audioMixerGroup = GetComponent<AudioSource>().outputAudioMixerGroup;
}

private void PlaySound()
{
    bool value = _audioMixerGroup.audioMixer.GetFloat("Sound", out float volume);
    volume = Mathf.Pow(10f, volume / 20); //converted from Log10
    AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position, volume);
}