public async Task RemoveSong(int index) { if (SongListStorage.CurrentPlaceInPlaylist == index) { Playlist.MovePrevious(); } Playlist.Items.RemoveAt(index); SongListStorage.PlaylistRepresentation.RemoveAt(index); int place = (int)Playlist.CurrentItemIndex; if (Playlist.CurrentItemIndex == 4294967295) //Magic number?? Perfectly totient. { SongListStorage.CurrentPlaceInPlaylist = 0; } else { SongListStorage.CurrentPlaceInPlaylist = place; } await SongListStorage.SaveNowPlaying(); /*if (SongListStorage.PlaylistRepresentation.Count == 0) * { * mediaPlayer.Source = new MediaPlaybackList(); * Currentart = DefaultArt; * Currentartist = ""; * Currenttitle = ""; * }*/ }
private async void Playlist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { if (!lastremovewasdel) { oldmoveindex = e.OldStartingIndex; } lastremovewasdel = false; } if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) //Bugged when adding songs { int newmoveindex = e.NewStartingIndex; if (oldmoveindex != -1) { try { var currenttime = Media.Instance.GetSongTime(); int place = SongListStorage.CurrentPlaceInPlaylist; if (oldmoveindex == place) { place = newmoveindex; } else { if (oldmoveindex < place && newmoveindex >= place) { place--; } if (oldmoveindex > place && newmoveindex <= place) { place++; } } var newplaylist = new MediaPlaybackList(); foreach (Song song in Playlist) { var mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(await song.GetFile())); newplaylist.Items.Add(mediaPlaybackItem); } newplaylist.CurrentItemChanged += Media.Instance.Playlist_CurrentItemChanged; currenttime = Media.Instance.GetSongTime(); Media.Instance.mediaPlayer.Source = newplaylist; Media.Instance.Playlist = newplaylist; newplaylist.MoveTo((uint)place); SongListStorage.CurrentPlaceInPlaylist = place; Media.Instance.SetSongTime(currenttime); await SongListStorage.SaveNowPlaying(); } catch (InvalidOperationException E) { //Debug.Writeline(); } } oldmoveindex = -1; } }
public async Task AddToPlaylist() { foreach (String songid in Songids) { await Media.Instance.AddSong(songid, false); } App.GetForCurrentView().NotificationMessage("Added " + Artist + " - " + Name + " to now playing."); await SongListStorage.SaveNowPlaying(); }
//Plays the playlist public async Task LoadNowPlaying(ObservableCollection <Song> Songs, int Pos, TimeSpan time) { mediaPlayer.Pause(); Playlist.Items.Clear(); //Clears the playlist SongListStorage.CurrentPlaceInPlaylist = 0; SongListStorage.PlaylistRepresentation.Clear(); //MAY BE BAD? Song currentsong = Songs[Pos - 1]; await AddSong(currentsong.ID, false); SetSongTime(time); bool b = mediaPlayer.PlaybackSession.CanPause; //mediaPlayer.Pause(); await UpdateNowPlaying(); //bool k .CanPause; int counter = 0; for (int i = 0; i < Songs.Count; i++) { var song = Songs[i]; var title = song.Title; var file = await song.GetFile(); if (file != null) { var mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(file)); if (i < Pos - 1) { Playlist.Items.Insert(counter, mediaPlaybackItem); SongListStorage.PlaylistRepresentation.Insert(counter, song); counter++; } if (i > Pos - 1) { Playlist.Items.Add(mediaPlaybackItem); SongListStorage.PlaylistRepresentation.Add(song); } } } await UpdateNowPlaying(); //var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; //Listeneventcount = (int)localSettings.Values["Listeneventcount"]; //SongListenInDB = (bool)localSettings.Values["nowplayingtime"]; await SongListStorage.SaveNowPlaying(); }
/// <summary> /// Invoked when application execution is being suspended. Application state is saved /// without knowing whether the application will be terminated or resumed with the contents /// of memory still intact. /// </summary> /// <param name="sender">The source of the suspend request.</param> /// <param name="e">Details about the suspend request.</param> private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); //TODO: Save application state and stop any background activity bool k = false; while (!k) { k = await SongListStorage.SaveNowPlaying(); } //await SongListStorage.SaveFlavours(); deferral.Complete(); }
//Plays the specified song public async Task PlaySong(string songid) { Playlist.Items.Clear(); var song = SongListStorage.SongDict[songid]; var file = await song.GetFile(); var mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(await song.GetFile())); Playlist.Items.Add(mediaPlaybackItem); mediaPlayer.Play(); SongListStorage.PlaylistRepresentation.Clear(); SongListStorage.PlaylistRepresentation.Add(SongListStorage.SongDict[songid]); await SongListStorage.SaveNowPlaying(); }
//Plays the playlist public async Task PlayPlaylist(ObservableCollection <Song> Songs, int Pos, string songid, bool play) { mediaPlayer.Pause(); Playlist.Items.Clear(); //Clears the playlist SongListStorage.CurrentPlaceInPlaylist = 0; SongListStorage.PlaylistRepresentation.Clear(); //MAY BE BAD? foreach (Song song in Songs) { await AddSong(song.ID, false); if (play) { mediaPlayer.Play(); } else { mediaPlayer.Pause(); } } if (Pos > 1 && SongListStorage.PlaylistRepresentation.Count >= Pos) { Playlist.MoveTo((uint)Pos - 1); } else { if (songid != null && songid != "") { for (int i = 0; i < SongListStorage.PlaylistRepresentation.Count; i++) { if (SongListStorage.PlaylistRepresentation[i].ID == songid) { Playlist.MoveTo((uint)i); break; } } } } if (play) { mediaPlayer.Play(); } else { mediaPlayer.Pause(); } //if (!play) mediaPlayer.Pause(); await SongListStorage.SaveNowPlaying(); }
//Appends a song to the playlist. public async Task AddSong(string songid, bool save) { if (SongListStorage.SongDict.ContainsKey(songid)) { var song = SongListStorage.SongDict[songid]; var file = await song.GetFile(); if (file != null) { var mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(file)); Playlist.Items.Add(mediaPlaybackItem); SongListStorage.PlaylistRepresentation.Add(SongListStorage.SongDict[songid]); } if (save) { await SongListStorage.SaveNowPlaying(); } } }