private int AddSong(int songId) { // Keep queue trimmed if (LookupMap.Count >= MAX_QUEUE_SIZE) { PlayQueueEntryModel head = null; if (PlaybackQueue.Count > 0) { head = PlaybackQueue.First(); } if (head != null) { if (CurrentPlaybackQueueEntryId == head.RowId) { // TODO: #6 raise alert about queue being full return(-1); } else { RemoveEntry(head.RowId); } } } PlayQueueEntryModel currentTail = null; if (PlaybackQueue.Count > 0) { currentTail = PlaybackQueue.Last(); } PlayQueueEntryTable newPlayQueueEntry; if (currentTail == null) { newPlayQueueEntry = new PlayQueueEntryTable(songId, 0, 0); DatabaseManager.Current.AddPlayQueueEntry(newPlayQueueEntry); } else { newPlayQueueEntry = new PlayQueueEntryTable(songId, 0, currentTail.RowId); DatabaseManager.Current.AddPlayQueueEntry(newPlayQueueEntry); currentTail.NextId = newPlayQueueEntry.RowId; } PlayQueueEntryModel newEntry = new PlayQueueEntryModel(newPlayQueueEntry); LookupMap.Add(newEntry.RowId, newEntry); PlaybackQueue.Add(newEntry); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); return(newEntry.RowId); }
public void Populate() { Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Calling Popuplate"); List <PlayQueueEntryTable> allEntries = DatabaseManager.Current.FetchPlayQueueEntries(); PlayQueueEntryModel head = null; foreach (PlayQueueEntryTable playQueueEntry in allEntries) { PlayQueueEntryModel newEntry = new PlayQueueEntryModel(playQueueEntry); LookupMap.Add(newEntry.RowId, newEntry); if (newEntry.PrevId == 0) { DebugHelper.Assert(new CallerInfo(), head == null, "Second head found in play queue!!!"); head = newEntry; } } PlayQueueEntryModel currentLocation = head; while (currentLocation != null && PlaybackQueue.Count < LookupMap.Count) { PlaybackQueue.Add(currentLocation); if (LookupMap.ContainsKey(currentLocation.NextId)) { currentLocation = LookupMap[currentLocation.NextId]; } else { currentLocation = null; } } DebugHelper.Assert(new CallerInfo(), currentLocation == null, "Circular reference found in Play Queue"); DebugHelper.Assert(new CallerInfo(), PlaybackQueue.Count == LookupMap.Count, "Missing element found in Play Queue"); CurrentPlaybackQueueEntryId = ApplicationSettings.GetSettingsValue <int>(ApplicationSettings.CURRENT_PLAYQUEUE_POSITION, 0); if (CurrentPlaybackQueueEntryId == 0) { ResetPlayerToStart(); } }
public async Task <QueueSong> AddToQueueAsync(Song song, QueueSong position = null, bool shuffleInsert = true) { if (song == null) { return(null); } var rnd = new Random(DateTime.Now.Millisecond); QueueSong prev = null; QueueSong shufflePrev = null; QueueSong next = null; QueueSong shuffleNext = null; var shuffleIndex = -1; var normalIndex = -1; if (position != null) { shuffleIndex = ShufflePlaybackQueue.IndexOf(position) + 1; normalIndex = PlaybackQueue.IndexOf(position) + 1; } var insert = normalIndex > -1 && normalIndex < PlaybackQueue.Count; var insertShuffle = shuffleIndex > -1 && shuffleInsert; var shuffleLastAdd = shuffleIndex == ShufflePlaybackQueue.Count; if (insert) { next = PlaybackQueue.ElementAtOrDefault(normalIndex); if (next != null) { _lookupMap.TryGetValue(next.PrevId, out prev); } } else { prev = PlaybackQueue.LastOrDefault(); } if (insertShuffle) { if (shuffleLastAdd) { shufflePrev = ShufflePlaybackQueue.ElementAtOrDefault(ShufflePlaybackQueue.Count - 1); } else { shuffleNext = ShufflePlaybackQueue.ElementAtOrDefault(shuffleIndex); if (shuffleNext != null) { _lookupMap.TryGetValue(shuffleNext.ShufflePrevId, out shufflePrev); } } } else { if (ShufflePlaybackQueue.Count > 1) { shuffleIndex = rnd.Next(1, ShufflePlaybackQueue.Count - 1); shuffleNext = ShufflePlaybackQueue.ElementAt(shuffleIndex); _lookupMap.TryGetValue(shuffleNext.ShufflePrevId, out shufflePrev); } else { shuffleLastAdd = true; shufflePrev = prev; } } // Create the new queue entry var newQueue = new QueueSong { SongId = song.Id, NextId = next == null ? 0 : next.Id, PrevId = prev == null ? 0 : prev.Id, ShuffleNextId = shuffleNext == null ? 0 : shuffleNext.Id, ShufflePrevId = shufflePrev == null ? 0 : shufflePrev.Id, Song = song }; // Add it to the database await _bgSqlService.InsertAsync(newQueue).ConfigureAwait(false); if (next != null) { // Update the prev id of the queue that was replaced next.PrevId = newQueue.Id; await _bgSqlService.UpdateItemAsync(next).ConfigureAwait(false); } if (prev != null) { // Update the next id of the previous tail prev.NextId = newQueue.Id; await _bgSqlService.UpdateItemAsync(prev).ConfigureAwait(false); } if (shuffleNext != null) { shuffleNext.ShufflePrevId = newQueue.Id; await _bgSqlService.UpdateItemAsync(shuffleNext).ConfigureAwait(false); } if (shufflePrev != null) { shufflePrev.ShuffleNextId = newQueue.Id; await _bgSqlService.UpdateItemAsync(shufflePrev).ConfigureAwait(false); } // Add the new queue entry to the collection and map await _dispatcher.RunAsync( () => { if (insert) { try { PlaybackQueue.Insert(normalIndex, newQueue); } catch (ArgumentOutOfRangeException) { PlaybackQueue.Add(newQueue); } } else { PlaybackQueue.Add(newQueue); } if (shuffleLastAdd || !shuffleInsert) { ShufflePlaybackQueue.Add(newQueue); } else { try { ShufflePlaybackQueue.Insert(shuffleIndex, newQueue); } catch (ArgumentOutOfRangeException) { ShufflePlaybackQueue.Add(newQueue); } } }).ConfigureAwait(false); _lookupMap.TryAdd(newQueue.Id, newQueue); return(newQueue); }
private void LoadQueue(List <Song> songs) { var queue = _bgSqlService.SelectAll <QueueSong>(); QueueSong head = null; QueueSong shuffleHead = null; foreach (var queueSong in queue) { queueSong.Song = songs.FirstOrDefault(p => p.Id == queueSong.SongId); _lookupMap.TryAdd(queueSong.Id, queueSong); if (queueSong.ShufflePrevId == 0) { shuffleHead = queueSong; } if (queueSong.PrevId == 0) { head = queueSong; } } if (head != null) { for (var i = 0; i < queue.Count; i++) { if (_dispatcher != null) { _dispatcher.RunAsync(() => PlaybackQueue.Add(head)).Wait(); } else { PlaybackQueue.Add(head); } QueueSong value; if (head.NextId != 0 && _lookupMap.TryGetValue(head.NextId, out value)) { head = value; } else { break; } } } if (shuffleHead != null) { for (var i = 0; i < queue.Count; i++) { if (_dispatcher != null) { _dispatcher.RunAsync(() => ShufflePlaybackQueue.Add(shuffleHead)).Wait(); } else { ShufflePlaybackQueue.Add(shuffleHead); } QueueSong value; if (shuffleHead.ShuffleNextId != 0 && _lookupMap.TryGetValue(shuffleHead.ShuffleNextId, out value)) { shuffleHead = value; } else { break; } } } }
public static void QueueItem(VoicePlayerItem item) { lock (padlock) PlaybackQueue.Add(item); }