- Home /
2d PlayAudioOneShot inconsistent behavior
I am working on a game where I have prefabbed menus with buttons on them. each button calls into my singleton MainAudio, triggering PlaySoundEffectInstant (line 90) passing in the sound effect for button click.
MainAudio plays sound effects on one audiosource while music plays on a seperate AudioSource so that I may fade music in and out without interupting sound effects.
My problem occurs when I navigate between these menus, eventually the PlayOneShot stops working. I have Visual Studios Debugger running and can verify the calls to PlaySoundEffectInstant does indeed occur, it gets all the way to the line PlayOneShot(audioclip, volume)(line 59) and then... nothing. when it goes to another menu sometimes it works, sometimes nothing... but it gets all the way to that line. I don't know what is wrong. there is nothing that changes the volume or priority of the audio sources.
using DigitalRuby.Tween;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainAudio : MonoBehaviour
{
private static MainAudio instance = null;
public AudioSource sourceMusic = null;
public AudioSource sourceEffects = null;
private AudioClip defaultClip = null;
[Range(0.0f, 1.0f)]
public float sfVolume = 1.0f;
public float FadeDelay = 1;
static Coroutine FadingIn = null;
static Coroutine FadingOut = null;
private void Awake()
{
if (instance == null)
{
instance = this;
defaultClip = sourceMusic.clip;
DontDestroyOnLoad(gameObject);
}
else
{
sourceMusic.Stop();
Destroy(gameObject);
}
}
/// <summary>
/// Plays Music or sound effects immidiately
/// </summary>
/// <param name="audio"></param>
/// <param name="asSoundEffect"></param>
/// <param name="volume"></param>
public static void Play(AudioClip audio)
{
instance.PlayMusic(audio);
}
/// <summary>
/// Plays audio one shot for sond effects
/// </summary>
/// <param name="audio"></param>
/// <param name="volume"></param>
private void playSoundEffect(AudioClip audio, float volume = 0.0f)
{
if (!IsMuted())
{
instance.sourceEffects.PlayOneShot(audio, (volume <= 0.01f ? instance.sfVolume : volume));
}
}
/// <summary>
///
/// </summary>
/// <param name="audio"></param>
/// <param name="volume"></param>
public static void PlaySoundEffect(AudioClip audio, float volume = 0.0f)
{
instance.StartCoroutine(instance.SoundEffectCoroutine(audio, volume));
}
private IEnumerator SoundEffectCoroutine(AudioClip audio, float volume = 0.0f)
{
instance.playSoundEffect(audio, volume);
yield return new WaitForEndOfFrame();
}
public bool PlayingClip(AudioClip clip)
{
return sourceMusic.clip == clip;
}
public static bool IsPlayingClip(AudioClip backgroundMusic)
{
return instance.PlayingClip(backgroundMusic);
}
public void PlaySoundEffectInstant(AudioClip audio)
{
instance.playSoundEffect(audio, instance.sfVolume);
}
public void PlayMusic(AudioClip audio)
{
if (!IsMuted() && instance.sourceMusic.clip != audio)
{
instance.sourceMusic.Stop();
instance.sourceMusic.clip = audio;
instance.sourceMusic.Play();
}
}
public static bool IsMuted()
{
return PlayerPrefs.GetInt(LS.Common.TermStrings.AllAudioOn, 1) == 0;
}
public static void ToggleMuted(bool value)
{
PlayerPrefs.SetInt(LS.Common.TermStrings.AllAudioOn, value ? 1 : 0);
PlayerPrefs.Save();
instance.TogglePauseMusic(!value);
}
public void ToggleMutedInstance(bool value)
{
ToggleMuted(value);
}
public void TogglePauseMusic(bool isPaused)
{
if (sourceMusic.clip != null)
{
if (isPaused && sourceMusic.isPlaying)
{
sourceMusic.Pause();
}
else if (!isPaused)
{
sourceMusic.UnPause();
}
}
}
/// <summary>
/// Plays new Music clip by fading from one to another.
/// </summary>
/// <param name="clip">New Music to play</param>
public static void PlayWithFade(AudioClip clip)
{
instance.StartCoroutine(instance.FadeToMusic(clip));
}
private IEnumerator FadeToMusic(AudioClip clip)
{
yield return FadeOut();
sourceMusic.Stop();
sourceMusic.clip = clip;
sourceMusic.Play();
yield return FadeIn();
}
public static void FadeMusicOut()
{
if (FadingOut == null)
{
if (FadingIn != null)
{
instance.StopCoroutine(FadingIn);
FadingIn = null;
}
FadingOut = instance.StartCoroutine(instance.FadeOut());
}
}
public static void FadeMusicIn()
{
if (FadingIn == null)
{
if(FadingOut != null)
{
instance.StopCoroutine(FadingOut);
FadingOut = null;
}
FadingIn = instance.StartCoroutine(instance.FadeIn());
}
}
private IEnumerator FadeOut()
{
if (sourceMusic.volume <= 0.0f)
{
FadingOut = null;
yield break;
}
FloatTween tween = TweenFactory.Tween(null, sourceMusic.volume, 0, FadeDelay, TweenScaleFunctions.Linear, null);
sourceMusic.volume = 1.0f;
while (!tween.Update(Time.deltaTime))
{
sourceMusic.volume = tween.CurrentValue;
yield return new WaitForEndOfFrame();
}
sourceMusic.volume = 0.0f;
FadingOut = null;
}
private IEnumerator FadeIn()
{
if (sourceMusic.volume >= 1.0f)
{
FadingIn = null;
yield break;
}
FloatTween tween = TweenFactory.Tween(null, sourceMusic.volume, 1, FadeDelay, TweenScaleFunctions.Linear, null);
sourceMusic.volume = 0.0f;
while (!tween.Update(Time.deltaTime))
{
sourceMusic.volume = tween.CurrentValue;
yield return new WaitForEndOfFrame();
}
sourceMusic.volume = 1.0f;
FadingIn = null;
}
}