Пример #1
0
        public bool Play(float volume, float pitch, float pan)
        {
            if (MasterVolume > 0.0f)
            {
                if (playing == null)
                {
                    playing      = new List <SoundEffectInstance>();
                    available    = new List <SoundEffectInstance>();
                    toBeRecycled = new List <SoundEffectInstance>();
                }
                else
                {
                    // Lets cycle through our playing list and see if any are stopped
                    // so that we can recycle them.
                    if (playing.Count > 0)
                    {
                        foreach (var instance2 in playing)
                        {
                            if (instance2.State == SoundState.Stopped)
                            {
                                toBeRecycled.Add(instance2);
                            }
                        }
                    }
                }

                SoundEffectInstance instance = null;
                if (toBeRecycled.Count > 0)
                {
                    foreach (var recycle in toBeRecycled)
                    {
                        available.Add(recycle);
                        playing.Remove(recycle);
                    }
                    toBeRecycled.Clear();
                }
                if (available.Count > 0)
                {
                    instance = available[0];
                    playing.Add(instance);
                    available.Remove(instance);
                    //System.Console.WriteLine("from pool = " + playing.Count);
                }
                else
                {
                    instance = CreateInstance();
                    playing.Add(instance);
                    //System.Console.WriteLine("pooled = "  + playing.Count);
                }
                instance.Volume = volume;
                instance.Pitch  = pitch;
                instance.Pan    = pan;
                return(instance.TryPlay());
            }
            return(false);
        }