/// <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); } }
/// <summary> /// Play next song /// </summary> public void Next(string requester) { if (_currentSong != null) { _currentSong.Song.SkippedBy = requester; } if (_queue.Count > 0) { lock (lockObject) { //Take next song from queue _currentSong = _queue.Current.First(); _queue.Remove(_currentSong, requester, _currentSong.Song.TempId); } } else { //Take random song _currentSong = _songLibrary.GetRandomSong(); } _currentSongStart = DateTime.Now; if (_currentSong != null) { lock (lockObject) { try { _mediaDevice.PlaySong(_currentSong.Song.FileName); _currentSong.Song.LastRequester = _currentSong.RequesterName.Equals("randomizer", StringComparison.OrdinalIgnoreCase) ? string.Empty : _currentSong.RequesterName; _currentSong.Song.SkippedBy = string.Empty; _currentSong.Song.LastPlayDateTime = DateTime.Now; } catch { try { Thread.Sleep(50); _mediaDevice.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, "randomizer", null); 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); }
/// <summary> /// Play next song /// </summary> public void Next(string requester) { if (_currentSong != null) _currentSong.Song.SkippedBy = requester; if (_queue.Count > 0) { lock (lockObject) { //Take next song from queue _currentSong = _queue.Current.First(); _queue.Remove(_currentSong, requester, _currentSong.Song.TempId); } } else { //Take random song _currentSong = _songLibrary.GetRandomSong(); } _currentSongStart = DateTime.Now; if (_currentSong != null) { lock (lockObject) { try { _mediaDevice.PlaySong(_currentSong.Song.FileName); _currentSong.Song.LastRequester = _currentSong.RequesterName.Equals("randomizer", StringComparison.OrdinalIgnoreCase) ? string.Empty : _currentSong.RequesterName; _currentSong.Song.SkippedBy = string.Empty; _currentSong.Song.LastPlayDateTime = DateTime.Now; } catch { try { Thread.Sleep(50); _mediaDevice.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, string requester, string currentSongId) { Remove(requestedSong.Song.TempId, requester, currentSongId); }
/// <summary> /// Update method /// </summary> public void Update() { while (_running) { try { bool isPlaying; lock (lockObject) { isPlaying = _mediaDevice.IsPlaying; } if (!isPlaying) { Next("randomizer"); } } catch { } string status; if (SongPlayerFactory.GetSongPlayer().PlayerStatus.RequestedSong != null) { status = string.Format("Currently playing at volume {2}: {0} sec -> {1}", SongPlayerFactory.GetSongPlayer().PlayerStatus.Position, SongPlayerFactory.GetSongPlayer().PlayerStatus.RequestedSong.Song.GetArtistAndTitle(), SongPlayerFactory.GetSongPlayer().Volume); } 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); } } // when not scanning for songs, clear the queue of unavailable songs if (!_songLibrary.ScanRunning) { ClearQueue(); } if (!_songLibrary.ScanLibrary()) { Thread.Sleep(50); } else { Thread.Sleep(10); } } }