public SoundPlaybackLifetime(MediaPlayer player, bool loop, SoundThread soundThread) { this.Player = player; this.loop = loop; this.soundThread = soundThread; player.MediaEnded += Player_MediaEnded; if (loop) { player.Volume = .1; } else { player.Volume = 1; } player.Play(); /* * * soundThread.Scene.SubscribeForLifetime(nameof(Scene.SpeedFactor), () => * { * soundThread.EnqueueSoundThreadAction(() => * { * player.SpeedRatio = soundThread.Scene.SpeedFactor == 1 ? 1 : .4; * }); * * }, this.LifetimeManager); */ }
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; startDeferred.Resolve(); Dispatcher.Run(); }
protected override void Dispose(bool disposing) { base.Dispose(disposing); soundThread.EnqueueSoundThreadAction(() => { lock (Player) { SoundThread.AssertSoundThread(); Player.Stop(); lock (soundThread.CurrentlyPlayingSounds) { soundThread.CurrentlyPlayingSounds.Remove(this); } } }); }
public SoundProvider() { SoundThread = new SoundThread(); SoundThread.Start(); }