InAudio Documentation

Advanced Audio Plugin for Unity

User Tools

Site Tools


examples:audiocodecontrol

Audio Code Control

Following the quickstart, you should now know how play audio. This example will detail how to control the playback further via code. Using InAudio.Play(…), InAudio.PlayFollowing(…) and InAudio.PlayAtPosition(…) you have two options to control various properties like volume, spatial blend and such.

The two ways of controlling the audio values are not mutually exclusive and can both be used at the same time or let all the values be controlled via the audio window.

Audio Parameters

In the methods mentioned above, there is an optional parameter called AudioParameters. This can be set up in the inspector or via code. Using this you can control directly set the audio's values and override the audio mixer. It is important to note that changing this after calling any Play(), does not affect the already playing node!

using UnityEngine;
public class ParameterExample : MonoBehaviour
{
    public InAudioNode gunshot;
    public InAudioNode footstep;

    public AudioParameters audioParameters;

    void OnEnable()
    {
        //Can also be set as audioParameters.Volume = 0.5f;
        InAudio.Play(gameObject, gunshot, audioParameters);
       
    }
}

Direct Player Control

This approach uses the InPlayer. It is returned when you play an audio node and allows you to modify the parameters as the sound is playing.

public class ParameterExample2 : MonoBehaviour
{
    public InAudioNode gunshot;
    public InAudioNode footstep;

    public AudioParameters audioParameters;

    void OnEnable()
    {
        InPlayer player = InAudio.Play(gameObject, gunshot);
        player.Volume = 0.5f;
        player.SpatialBlend = 0.2f;
        player.PanStereo = 0.0f;
        player.AudioMixer = null;
        player.OnCompleted += (GO, audioNode) => Debug.Log("Node " + audioNode.GetName + " has finished playing");

        //OR

        player.SetFromAudioParameters(audioParameters);
    }
}

examples/audiocodecontrol.txt · Last modified: 2015/07/12 17:08 by peterolsted