/// <summary> /// Add song to queue /// </summary> public void Add(RequestedSong requestedSong) { lock (lockObject) { //Do not allow adding same song twice... if (_requestedSongs.Any(r => r.Song == requestedSong.Song)) { return; } for (int i = 1; i < _requestedSongs.Count; i++) { if (_requestedSongs[i].RequesterName == requestedSong.RequesterName) { continue; } var groupedRequestedSongs = _requestedSongs.Take(i + 1).GroupBy(r => r.RequesterName).OrderByDescending(g => g.Count()); var maxRequesterUntilNow = groupedRequestedSongs.FirstOrDefault(); if (maxRequesterUntilNow.First().RequesterName != requestedSong.RequesterName && maxRequesterUntilNow.Count() > 1 && (groupedRequestedSongs.FirstOrDefault(g => g.First().RequesterName == requestedSong.RequesterName) == null || groupedRequestedSongs.FirstOrDefault(g => g.First().RequesterName == requestedSong.RequesterName).Count() < (maxRequesterUntilNow.Count() - 1))) { _requestedSongs.Insert(i, requestedSong); return; } } _requestedSongs.Add(requestedSong); } }
/// <summary> /// Add song to queue /// </summary> public void Add(RequestedSong requestedSong) { lock (lockObject) { //Do not allow adding same song twice... if (_requestedSongs.Any(r => r.Song == requestedSong.Song)) return; for (int i = 1; i < _requestedSongs.Count; i++) { if (_requestedSongs[i].RequesterName == requestedSong.RequesterName) continue; var groupedRequestedSongs = _requestedSongs.Take(i + 1).GroupBy(r => r.RequesterName).OrderByDescending(g => g.Count()); var maxRequesterUntilNow = groupedRequestedSongs.FirstOrDefault(); if (maxRequesterUntilNow.First().RequesterName != requestedSong.RequesterName && maxRequesterUntilNow.Count() > 1 && (groupedRequestedSongs.FirstOrDefault(g => g.First().RequesterName == requestedSong.RequesterName) == null || groupedRequestedSongs.FirstOrDefault(g => g.First().RequesterName == requestedSong.RequesterName).Count() < (maxRequesterUntilNow.Count() - 1))) { _requestedSongs.Insert(i, requestedSong); return; } } _requestedSongs.Add(requestedSong); } }
public void Update() { while (_running) { if (!_songLibrary.ScanLibrary()) { Thread.Sleep(500); } try { WMPPlayState playState; lock (lockObject) { playState = player.playState; } if (playState == WMPPlayState.wmppsStopped || playState == WMPPlayState.wmppsUndefined) { Next(null); } } catch { } string status; if (SongPlayerFactory.GetSongPlayer().PlayerStatus.RequestedSong != null) { status = string.Format("Currently playing: {0} sec -> {1}", SongPlayerFactory.GetSongPlayer().PlayerStatus.Position, SongPlayerFactory.GetSongPlayer().PlayerStatus.RequestedSong.Song.GetArtistAndTitle()); } else { status = "No song playing..."; } OnPlayerStatusChanged(status); int minimalsonginqueue; if (!int.TryParse(SongPlayerFactory.GetConfigFile().GetValue("player.minimalsonginqueue"), out minimalsonginqueue)) { minimalsonginqueue = 0; } //Enqueue random song when the queue is empty and the current song is almost finished if (_currentSong != null && _queue.Count < minimalsonginqueue + ((int)(DateTime.Now - _currentSongStart).TotalSeconds + 20 > _currentSong.Song.Duration ? 1 : 0)) { RequestedSong requestedSong = _songLibrary.GetRandomSong(); if (requestedSong != null) { Enqueue(requestedSong.Song, requestedSong.RequesterName); } } } }
public void Next(string requesterName) { if (!ClientAllowed(requesterName)) { return; } if (_queue.Count > 0) { lock (lockObject) { //Take next song from queue _currentSong = _queue.Current.First(); _queue.Remove(_currentSong); } } else { //Take random song _currentSong = _songLibrary.GetRandomSong(); } _currentSongStart = DateTime.Now; if (_currentSong != null) { lock (lockObject) { try { player.URL = _currentSong.Song.FileName; } catch { try { Thread.Sleep(50); player.controls.stop(); //Try to stop the player... if this fails, just ignore... } catch { //ignore this } } } } }
public void TestFairQueue() { FairQueue queue = new FairQueue(); var first = new RequestedSong() { RequestedDate = DateTime.Now, Song = new Song() { FileName="A.mp3" }, RequesterName="A" }; queue.Add(first); Assert.AreEqual(queue.Count, 1); Assert.IsNotNull(queue.Current.First()); queue.Add(new RequestedSong() { RequestedDate = DateTime.Now.AddSeconds(1), Song = new Song() { FileName = "B.mp3" }, RequesterName = "A" }); Assert.AreEqual(queue.Count, 2); Assert.IsNotNull(queue.Current.First()); Assert.AreEqual("A.mp3", queue.Current.First().Song.FileName); queue.Add(new RequestedSong() { RequestedDate = DateTime.Now.AddSeconds(1), Song = new Song() { FileName = "C.mp3" }, RequesterName = "B" }); Assert.AreEqual(queue.Count, 3); Assert.IsNotNull(queue.Current.First()); Assert.AreEqual("A.mp3", queue.Current.First().Song.FileName); Assert.AreEqual("C.mp3", queue.Current.ElementAt(1).Song.FileName); Assert.AreEqual("B.mp3", queue.Current.ElementAt(2).Song.FileName); queue.Add(new RequestedSong() { RequestedDate = DateTime.Now.AddSeconds(1), Song = new Song() { FileName = "D.mp3" }, RequesterName = "C" }); Assert.AreEqual(queue.Count, 4); Assert.IsNotNull(queue.Current.First()); Assert.AreEqual("A.mp3", queue.Current.First().Song.FileName); Assert.AreEqual("C.mp3", queue.Current.ElementAt(1).Song.FileName); Assert.AreEqual("D.mp3", queue.Current.ElementAt(2).Song.FileName); Assert.AreEqual("B.mp3", queue.Current.ElementAt(3).Song.FileName); queue.Remove(first); Assert.AreEqual(queue.Count, 3); Assert.IsNotNull(queue.Current.First()); Assert.AreEqual("C.mp3", queue.Current.ElementAt(0).Song.FileName); Assert.AreEqual("D.mp3", queue.Current.ElementAt(1).Song.FileName); Assert.AreEqual("B.mp3", queue.Current.ElementAt(2).Song.FileName); }
public void Next(string requesterName) { if (!ClientAllowed(requesterName)) return; if (_queue.Count > 0) { lock (lockObject) { //Take next song from queue _currentSong = _queue.Current.First(); _queue.Remove(_currentSong); } } else { //Take random song _currentSong = _songLibrary.GetRandomSong(); } _currentSongStart = DateTime.Now; if (_currentSong != null) { lock (lockObject) { try { player.URL = _currentSong.Song.FileName; } catch { try { Thread.Sleep(50); player.controls.stop(); //Try to stop the player... if this fails, just ignore... } catch { //ignore this } } } } }
/// <summary> /// Remove song from queue /// </summary> /// <param name="requestedSong">Song to delete</param> public void Remove(RequestedSong requestedSong) { Remove(requestedSong.Song.TempId); }