Code works in editor, but not on Android device(audio mixer)
Hey have created a script which should make a transition between two audio mixer groups and it works fine if I test it in the editor: the first groups volume decreases and the other groups volume increases. After the transition the volumes don't change in the editor, but if I run my game on my HTC One M9 the transition works and after it the first groups volume jumps to 100% although it should be 0.
The Coroutine which makes the transition is the changeChannel IEnumerator
at the bottom of the script.
If you need an example you can download https://www.dropbox.com/s/riwis1zj4utjb9l/Android%20sound%20test.zip?dl=0, this is an example project using the code below.
Do you know why it doesn't work?
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
public class AudioScr : MonoBehaviour {
public AudioMixer masterMixer;
public float changeMusicChannelTime;
private float lastPitch=0, currentPitch,currentTime,progress;
// Use this for initialization
void Start () {
masterMixer.SetFloat ("MmusicVol", 0);
masterMixer.SetFloat ("IGmusicVol", -50);
}
// Update is called once per frame
void Update () {
currentPitch = Time.timeScale;
if (currentPitch != lastPitch) {
masterMixer.SetFloat ("IngameMusicPitch", currentPitch);
}
if (currentPitch == 0 && lastPitch != 0) {
StartCoroutine (changeChannel (false));
} else if (currentPitch != 0 && lastPitch == 0) {
StartCoroutine (changeChannel (true));
}
lastPitch = currentPitch;
}
public void changeChannelTimeAdjust(float newTime){
changeMusicChannelTime = newTime * 5+0.1f;
}
public void SetEffectVolume(float effectVol){
if (effectVol != 0) {
effectVol = 20 * Mathf.Log10 (effectVol);
} else {
effectVol = -50;
}
masterMixer.SetFloat ("effectVolume", effectVol);
}
public void SetMusicVolume(float musicVol){
if (musicVol != 0) {
musicVol = 20 * Mathf.Log10 (musicVol);
} else {
musicVol = -50;
}
masterMixer.SetFloat ("musicVolume", musicVol);
}
IEnumerator changeChannel(bool purpose){
Debug.Log ("Start channel switch: "+purpose);
do{
currentTime += Time.unscaledDeltaTime;
progress = currentTime / changeMusicChannelTime;
if (purpose) {
//from Menue to Ingame
float MmusicVol = 20 * Mathf.Log10 (1-progress);
float IGmusicVol = 20 * Mathf.Log10 (progress);
if (progress == 0) {
IGmusicVol = -50;
} else if (progress == 1) {
MmusicVol = -50;
}
masterMixer.SetFloat ("MmusicVol", MmusicVol);
masterMixer.SetFloat ("IGmusicVol", IGmusicVol);
} else {
//from Ingame to Menue
float MmusicVol = 20 * Mathf.Log10 (progress);
float IGmusicVol = 20 * Mathf.Log10 (1 - progress);
if (progress == 0) {
MmusicVol = -50;
}else if (progress == 1) {
IGmusicVol = -50;
}
masterMixer.SetFloat ("MmusicVol", MmusicVol);
masterMixer.SetFloat ("IGmusicVol", IGmusicVol);
}
yield return null;
}while (progress < 1);
progress = 0;
currentTime = 0;
}
}
Answer by El__Nacho · May 18, 2016 at 06:04 PM
Oh I found a solution :D In line 68 and 80 I was using the progress==1
condition, so if the value wasn't exactly 1 the code wouldn't work. The volume dropped below -80dB and the editor has handled it for some reason but my phone hasn't and reseted it to 0dB. Now I have replaced it by progress>=1
and it works.