I’m losing all computer audio (i.e. applications outside Editor & build) at runtime when simply fading in/out audio volume tied to a character’s health. There are 2 looping audio clips essentially crossfading, and the game audio is loaded via Resources.LoadAsync() and clips set to Load Into Memory.
During gameplay, a character is chosen with a cursor. As the cursor holds on the character, it charges its health up to a threshold where it can be selected. When selected, it runs along a path, continually losing health over time unless it hits a trigger, at which point the health jumps up. The health is tied to an ambient clip and a music clip, and they effectively crossfade based on health.
At some point along this path, all computer audio will cut out, and often not right away but it varies. Then when I hit a trigger, which increases health and changes the volume levels, all audio including game audio will return to normal levels for a split second (0.1-0.3s roughly) then go out again. When the health runs out, the camera moves back to the start position. When the camera gets closer to the start position, all audio returns. But then when I move the cursor to select a character, which starts crossfading again since its charging up a character, all audio will cut out again. If I stop the application in the editor, the audio returns, and if I play again audio levels will be normal until I get to the same point again in the scene.
The code that crossfades is pretty simple, note externalVolumeFactor ultimately changes the volume on the AudioSource:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameMusic : MonoBehaviour {
public Animal upAnimal;
public Animal downAnimal;
public Animal leftAnimal;
public Animal rightAnimal;
public ManagedSound center;
public ManagedSound up;
public ManagedSound down;
public ManagedSound left;
public ManagedSound right;
VariableGain vgUp;
VariableGain vgDown;
VariableGain vgLeft;
VariableGain vgRight;
public float[] musicLevels;
private float[] lerpValues = new float[4];
float furthestFromCenter = 0f;
void Start()
{
musicLevels = new float[4];
//if (center != null) center.FadeIn(3);
if(up != null) vgUp = up.gameObject.GetComponent<VariableGain>();
if(down != null) vgDown = down.gameObject.GetComponent<VariableGain>();
if(left != null) vgLeft = left.gameObject.GetComponent<VariableGain>();
if(right != null) vgRight = right.gameObject.GetComponent<VariableGain>();
}
// Update is called once per frame
void Update ()
{
if(upAnimal && downAnimal && leftAnimal && rightAnimal)
{
if(center != null)
{
lerpValues = new float[] {upAnimal.orderLerp, downAnimal.orderLerp, rightAnimal.orderLerp, leftAnimal.orderLerp};
System.Array.Sort(lerpValues);
furthestFromCenter = lerpValues[3];
center.externalVolumeFactor = Mathf.Clamp01(Mathf.Pow (1f - furthestFromCenter,.75f));
}
if(up != null)
{
up.externalVolumeFactor = Mathf.Clamp01(Mathf.Pow(upAnimal.orderLerp, 2f));
}
if(down != null)
{
down.externalVolumeFactor = Mathf.Clamp01(Mathf.Pow(downAnimal.orderLerp, 2f));
}
if(right != null)
{
right.externalVolumeFactor = Mathf.Clamp01(Mathf.Pow(rightAnimal.orderLerp, 2f));
}
if(left != null)
{
left.externalVolumeFactor = Mathf.Clamp01(Mathf.Pow(leftAnimal.orderLerp, 2f));
}
}
if(vgUp != null) musicLevels[0] = vgUp.percentOfPeakLevel;
if(vgDown != null) musicLevels[1] = vgDown.percentOfPeakLevel;
if(vgLeft != null) musicLevels[2] = vgLeft.percentOfPeakLevel;
if(vgRight != null) musicLevels[3] = vgRight.percentOfPeakLevel;
}
public void StartMusic ()
{
StartCoroutine(doStartMusic());
}
IEnumerator doStartMusic ()
{
center.PlayAndFade(5f);
yield return new WaitForSeconds(2f);
up.PlayAndFade(1.5f);
down.PlayAndFade(1.5f);
left.PlayAndFade(1.5f);
right.PlayAndFade(1.5f);
}
public void CrossfadeMusic(float time)
{
float _time = time*.7f;
if(up != null) up.FadeOut (_time);
if(down != null) down.FadeOut (_time);
if(right != null) right.FadeOut (_time);
if(left != null) left.FadeOut (_time);
if(center != null) center.FadeOut (time);
}
}
It happens in the editor and in builds. I’m in Unity 4.6.8p2, on OS X 10.9.5.