public PlayViewModel() { ////if (IsInDesignMode) ////{ //// // Code runs in Blend --> create design time data. ////} ////else ////{ //// // Code runs "for real" ////} PropertyChanged += (sender, e) => { if (String.Equals(e.PropertyName, BPMPropertyName)) { ThreadPool.QueueUserWorkItem(new WaitCallback(o => { List<AnalyzedSong> songs; using (BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString)) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<AnalyzedSong>(p => p.AudioSummary); context.LoadOptions = dlo; context.ObjectTrackingEnabled = false; songs = context.AnalyzedSongs .Where(s => s.AudioSummary != null && Math.Abs((int)s.AudioSummary.Tempo - BPM) >= BPMTolerance) .Shuffle() .ToList(); } // Need to look up the actual MediaLibrary instances corresponding // to the songs we have in the database List<Song> mediaLibrarySongs = songs .Select<AnalyzedSong, Song>(song => song.ToMediaLibrarySong()) .Where(song => song != null) .ToList(); Messenger.Default.Send<NotificationMessage<List<Song>>>( new NotificationMessage<List<Song>>(mediaLibrarySongs, null)); })); } }; Messenger.Default.Register<PropertyChangedMessage<bool>>(this, m => { if (String.Equals(m.PropertyName, SongsAnalyzedPropertyName) && m.NewValue) { SongsAnalyzed = true; } }); Messenger.Default.Register<NotificationMessage<List<Song>>>( this, m => { BetterMediaPlayer p = new BetterMediaPlayer(m.Content); DispatcherHelper.InvokeAsync(() => Player = p); }); PlayCommand = new RelayCommand( () => { if (Player.State == MediaState.Paused) { Player.Resume(); } else if (Player.State == MediaState.Playing) { Player.Pause(); } else { Player.Play(); } }, () => true); // TODO Remember user setting BPM = 120; }
private void LoadSongs(bool continuePlay) { ThreadPool.QueueUserWorkItem(new WaitCallback(o => { Group<AnalyzedSong> match = null; if (songs != null) { match = songs .Where(g => BPM - g.Key > 0 && BPM - g.Key < 10) .FirstOrDefault(); } if(match == null) { DispatcherHelper.CheckBeginInvokeOnUI(() => { HaveSongs = false; }); } else { // Need to look up the actual MediaLibrary instances corresponding // to the songs we have in the database List<Song> mediaLibrarySongs = match.Items .Select<AnalyzedSong, Song>(song => song.MediaLibrarySong) .ToList(); BetterMediaPlayer p = new BetterMediaPlayer(mediaLibrarySongs); p.PropertyChanged += Player_PropertyChanged; DispatcherHelper.CheckBeginInvokeOnUI(() => { if(Player != null) { Player.UnregisterEvents(); } Player = p; HaveSongs = true; if (continuePlay) { Player.Play(); } }); } })); }