/// <summary> /// Play from the file path. /// </summary> /// <param name="path">The path of music file</param> private bool Play(string path) { Player.Stop(); if (File.Exists(path)) { Player.Source = new Uri(path); Player.Play(); State = PlayState.PLAY; timer.Start(); PlayIcon.Kind = MaterialDesignThemes.Wpf.PackIconKind.Pause; return(true); } else { // Path doesn't exist, find from List and remove it. var index = Musics.ToList().FindIndex(a => a.Path.Equals(path)); if (index != -1) { Musics.RemoveAt(index); SaveMusicList(); } return(false); } }
/// <summary> /// Read musics from json file. /// </summary> private void ReadMusics() { if (File.Exists(SavePath)) { try { // Read musics from json file. using (var file = new StreamReader(SavePath)) { string musics = file.ReadToEnd(); Musics = JsonConvert.DeserializeObject <ObservableCollection <Music> >(musics); } } catch (Exception e) { Console.WriteLine(e.Message); } for (var i = 0; i < Musics.Count; i++) { if (File.Exists(Musics[i].Path)) { try { // Read ID3Tag from mp3 file. var tag = new Mp3File(Musics[i].Path).TagHandler; Musics[i].AlbumImage = BitmapToBitmapImage(new Bitmap(tag.Picture)); } catch (Exception e) { Console.WriteLine(e.Message); Musics[i].AlbumImage = null; } } else { // Remove the music of which path doesn't exist. Musics.RemoveAt(i); i--; } } MusicList.DataContext = Musics; SaveMusicList(); } else if (!Directory.Exists("./Data")) { Directory.CreateDirectory("./Data"); File.Create(SavePath); } }