Exemple #1
0
 private SoundEffects()
 {
     if (Scene.Current == null)
     {
         throw new InvalidOperationException("No render loop on current thread");
     }
     SoundThread = new SoundThread(Scene.Current);
 }
        private void Run()
        {
            players = LoadSounds();

            Window hiddenWindow = new Window()
            {
                Width         = 0,
                Height        = 0,
                WindowStyle   = WindowStyle.None,
                ShowInTaskbar = false,
                ShowActivated = false
            };

            hiddenWindow.Visibility = Visibility.Hidden;
            hiddenWindow.Loaded    += (s, o) =>
            {
                _current = this;
                Thread.CurrentThread.IsBackground = true;
                DispatcherTimer t = new DispatcherTimer();
                t.Interval = TimeSpan.FromMilliseconds(1);
                t.Tick    += (s1, o1) =>
                {
                    Queue <SoundAction> toRun = new Queue <SoundAction>();
                    lock (soundQueue)
                    {
                        while (soundQueue.Count > 0)
                        {
                            toRun.Enqueue(soundQueue.Dequeue());
                        }
                    }

                    while (toRun.Count > 0)
                    {
                        var next = toRun.Dequeue();
                        if (next is StopSoundThreadAction)
                        {
                            t.Stop();
                            CurrentlyPlayingSounds.ToArray().ToList().ForEach(sound => sound.Dispose());
                            hiddenWindow.Close();
                            _current = null;
                        }
                        else
                        {
                            next.ToRun.Invoke();
                        }
                    }
                };
                t.Start();
            };
            hiddenWindow.Show();
            hiddenWindow.Visibility = Visibility.Hidden;
            Dispatcher.Run();
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            lock (Player)
            {
                SoundThread.AssertSoundThread();
                Player.Stop();

                lock (soundThread.CurrentlyPlayingSounds)
                {
                    soundThread.CurrentlyPlayingSounds.Remove(this);
                }
            }
        }
        public SoundPlaybackLifetime(MediaPlayer player, bool loop, SoundThread soundThread)
        {
            this.Player      = player;
            this.loop        = loop;
            this.soundThread = soundThread;

            player.MediaEnded += Player_MediaEnded;
            player.Play();


            soundThread.Scene.SubscribeForLifetime(nameof(Scene.SpeedFactor), () =>
            {
                soundThread.EnqueueSoundThreadAction(() =>
                {
                    player.SpeedRatio = soundThread.Scene.SpeedFactor == 1 ? 1 : .4;
                });
            }, this.LifetimeManager);
        }
Exemple #5
0
 public void PlaySound(string name)
 {
     SoundThread.Play(name, name == "music");
 }