From 2e66f0cc8a9ecd788543117575f1c2ea7b56bac2 Mon Sep 17 00:00:00 2001 From: roham shirchi Date: Tue, 30 Apr 2024 23:45:39 +0330 Subject: [PATCH] cleaned the codes, removed unnecessary optimizations, moved codes that change the UI into the main form codes, etc --- Media Player/AudioDataBase.cs | 217 +------ Media Player/FilterDuplicates.cs | 3 - Media Player/FormEditor.cs | 327 ----------- Media Player/Khi Player Listview Form.cs | 701 +++++++++++++---------- Media Player/PlayBackFunction.cs | 4 - Media Player/PlayList.cs | 4 - Media Player/SharedFieldsAndVariables.cs | 3 + 7 files changed, 434 insertions(+), 825 deletions(-) delete mode 100644 Media Player/FormEditor.cs diff --git a/Media Player/AudioDataBase.cs b/Media Player/AudioDataBase.cs index 67f05c9..13694e4 100644 --- a/Media Player/AudioDataBase.cs +++ b/Media Player/AudioDataBase.cs @@ -13,19 +13,7 @@ namespace Khi_Player /// For creating, writing to, and reading from the database and the manipulation of the elements within it. /// public class AudioDataBase - {/* - internal enum Playlists { allSongs, searchPlaylist, DynamicPlaylists }; - internal static Playlists CurrentPlaylist = (Playlists)Form1.CurrentPlaylist; - internal enum SortOrders { TitleSort, ArtistSort, AlbumSort, CustomSort }; - internal static int currentlyPlayingSongIndex = Form1.currentlyPlayingSongIndex; - internal static string[]? currentlySelectedSong = Form1.currentlySelectedSong; - internal static string? CurrentPlaylistName = Form1.CurrentPlaylistName; - internal static string applicationPath = System.Windows.Forms.Application.StartupPath; - internal static string albumArtsPath = applicationPath + "Album Arts\\"; - internal static string albumArtsThumbnailsPath = applicationPath + "Album Arts Thumbnails\\"; - internal static string allMusicDataBase = System.Windows.Forms.Application.StartupPath + "AllMusicDataBase.xml"; - //internal static string playListIni = System.Windows.Forms.Application.StartupPath + "PlaylistIni.xml"; - */ + { private static bool ThumbnailCallback() { return false; @@ -60,62 +48,13 @@ private static bool ThumbnailCallback() } else { - //might bring these back, idk, so not gonna remove them for now - /* - //getting a picture of the audio file - var tempPics = musicTags.Tag.Pictures; - - if (tempPics.Length > 0) - { - using (MemoryStream picConverter = new MemoryStream(tempPics[0].Data.Data)) - { - art = Image.FromStream(picConverter); - } - } - else { art = Properties.Resources.MusicArt_NoCover; } - - //to get the duration of the audio - using (AudioFileReader durationReader = new AudioFileReader(audioPath)) - { - if (musicTags.Properties.Duration.Hours < 1) - { - var mins = durationReader.TotalTime.Minutes.ToString(); - var secs = durationReader.TotalTime.Seconds.ToString("00"); - duration = mins + ":" + secs; - } - else - { - var hours = durationReader.TotalTime.Hours.ToString(); - var mins = durationReader.TotalTime.Minutes.ToString("00"); - var secs = durationReader.TotalTime.Seconds.ToString("00"); - duration = hours + ":" + mins + ":" + secs; - } - } - - //to get track number in Album - uint tempTrack = musicTags.Tag.Track; - if (tempTrack == 0) { trackNumber = ""; } - else { trackNumber = tempTrack.ToString(); } - if (musicTags.Tag.Lyrics != null) - { - lyrics = musicTags.Tag.Lyrics.ReplaceLineEndings(); - } - else { lyrics = ""; } - */ path = audioPath; - if (musicTags.Tag.Title == null) { System.IO.FileInfo sth = new System.IO.FileInfo(path); title = (string)sth.Name.Clone(); - - sth = null; - } - else - { - title = musicTags.Tag.Title; } - + else { title = musicTags.Tag.Title; } //for artists var allArtists = musicTags.Tag.Performers; if (allArtists.Length == 0) @@ -132,20 +71,11 @@ private static bool ThumbnailCallback() } artist = (string?)tempText.Text.Clone(); tempText.Dispose(); - allArtists = null; } - else //why? idk - { artist = (string?)musicTags.Tag.FirstPerformer.Clone(); } - + else { artist = (string?)musicTags.Tag.FirstPerformer.Clone(); } //For Album - if (musicTags.Tag.Album == null) - { - album = ""; - } - else - { - album = (string?)musicTags.Tag.Album.Clone(); - } + if (musicTags.Tag.Album == null) { album = ""; } + else { album = (string?)musicTags.Tag.Album.Clone(); } musicInfo[0] = title; musicInfo[1] = artist; @@ -165,11 +95,6 @@ private static bool ThumbnailCallback() } string[][]? addedMusicInfo = (string[][]?)tempMusicInfos.Clone(); - //to dispose - tempMusicInfos = null; - - GC.Collect(); - GC.WaitForPendingFinalizers(); return addedMusicInfo; } @@ -271,9 +196,6 @@ private static void XmlDataBaseWriter(string[][]? checkedPlaylistMusicData, stri playlistDatabase.Save(dataBaseWriter); dataBaseWriter.Dispose(); } - - //for disposal - playlistDatabase = null; } /// @@ -340,8 +262,6 @@ private static int WriteAudioDataBase(string[][] selectedMusicsInfo) art = (Image)Image.FromStream(picConverter).Clone(); thumbnail = art.GetThumbnailImage(60, 60, myCallback, 0); } - - tempPics = null; } else { @@ -384,16 +304,6 @@ private static int WriteAudioDataBase(string[][] selectedMusicsInfo) //for disposal musicData.Clear(); - musicData = null; - selectedMusicsData = null; - selectedMusicsInfo = null; - art = null; - thumbnail = null; - fileNames = null; - title = null; - artist = null; - album = null; - path = null; return addedCount; } @@ -499,18 +409,6 @@ private static int WriteAudioDataBase(string[][] selectedMusicsInfo) playlistDatabase.Save(dataBaseWriter); dataBaseWriter.Dispose(); } - - //for disposal - checkedPlaylistMusic = null; - playlistDatabase = null; - title = null; - artist = null; - album = null; - path = null; - artPath = null; - thumbnailPath = null; - playlistName = null; - return playlist; } else @@ -538,9 +436,6 @@ await Task.Run(() => { XmlDataBaseWriter(checkedPlaylistMusicData, playlist, nameOfPlaylist); }); - - //for disposal - checkedPlaylistMusicData = null; } } @@ -560,9 +455,6 @@ public static void WriteAudioDataBaseSync(string[][]? playlistMusicData, string { //write to data base XmlDataBaseWriter(checkedPlaylistMusicData, playlist, nameOfPlaylist); - - //for disposal - checkedPlaylistMusicData = null; } } @@ -603,10 +495,7 @@ public static (string?, string[][]?, Image[]?) ReadPlaylistDataBase(string? play tempAddedMusicInfos[z] = (string[])tempPlaylistMusicInfo[i].Clone(); z++; } - - dataBaseSerializer = null; } - else //in case of "complete", written like this for simplicity since it didn't need to be complicated as it is only a reading process { @@ -615,10 +504,8 @@ public static (string?, string[][]?, Image[]?) ReadPlaylistDataBase(string? play { tempPlaylistMusicInfo = (string[][])dataBaseSerializer.Deserialize(textReader); } - dataBaseSerializer = null; } playlistArts = GetMusicThumbnails(tempPlaylistMusicInfo); - return (playlistName, tempPlaylistMusicInfo, playlistArts); } @@ -641,7 +528,6 @@ public static (string?, string[][]?, Image[]?) ReadPlaylistDataBase(string? play { tempAllMusicInfos = (string[][])dataBaseSerializer.Deserialize(textReader); } - string[][]? tempAddedMusicInfos = new string[added][]; int z = 0; for (int i = tempAllMusicInfos.Length - added; i < tempAllMusicInfos.Length; i++) @@ -649,21 +535,15 @@ public static (string?, string[][]?, Image[]?) ReadPlaylistDataBase(string? play tempAddedMusicInfos[z] = (string[])tempAllMusicInfos[i].Clone(); z++; } - - dataBaseSerializer = null; return tempAddedMusicInfos; } - else { - XmlSerializer dataBaseSerializer = new XmlSerializer(typeof(string[][])); using (StreamReader textReader = new StreamReader(allMusicDataBase, Encoding.UTF8)) { tempAllMusicInfos = (string[][])dataBaseSerializer.Deserialize(textReader); } - - dataBaseSerializer = null; return tempAllMusicInfos; } } @@ -700,12 +580,7 @@ await Task.Run(() => XmlWriter dataBaseWriter = XmlWriter.Create(datastream, settings); playlistDatabase.Save(dataBaseWriter); dataBaseWriter.Close(); - dataBaseWriter = null; } - - //for disposal - playlistDatabase = null; - playlistDatabasePath = null; }); } @@ -734,7 +609,6 @@ public static (string?, string[][]?) ReadPlaylist(string? playlistPath) for (int i = 0; i < AllSongs.ChildNodes.Count; i++) { info = new string[6]; - info[0] = AllSongs.ChildNodes[i].ChildNodes[0].InnerText; info[1] = AllSongs.ChildNodes[i].ChildNodes[1].InnerText; info[2] = AllSongs.ChildNodes[i].ChildNodes[2].InnerText; @@ -751,9 +625,6 @@ public static (string?, string[][]?) ReadPlaylist(string? playlistPath) playlistData = null; } - //for disposal - PlaylistDatabase = null; - return (playlistName, playlistData); } @@ -795,13 +666,9 @@ private static bool RemoveSongFromDataBase(string[] item, bool fromAllPlaylists break; } } - else - { - existingDatabases.Add(database); - } + else { existingDatabases.Add(database); } } } - foreach (string? database in existingDatabases) { musicDatabase.Load(database); @@ -828,13 +695,6 @@ private static bool RemoveSongFromDataBase(string[] item, bool fromAllPlaylists } musicDatabase.Save(database); } - - //for disposal - musicDatabase = null; - similarItemsIndices = null; - title = null; - artist = null; - album = null; return isRemoved; } @@ -845,11 +705,12 @@ private static bool RemoveSongFromDataBase(string[] item, bool fromAllPlaylists /// /// /// - public static async void RemoveSongs(List toBeRemovedItemsIndices, bool wasPlaying = false) + /// + public static async void RemoveSongs(List toBeRemovedItemsIndices, bool wasPlaying = false, bool allItemsSelected = false) { await Task.Run(() => { - bool allItemsSelected = false; + bool removeFromAllPlaylists = false; List toBeRemovedItems = new List(); string[][]? playlist; @@ -879,10 +740,7 @@ await Task.Run(() => } List tempPlaylist = new List(); - if (allItemsSelected == true) - { - playlist = null; - } + if (allItemsSelected == true) { playlist = null; } else { //getting the info of the items that should be removed @@ -919,17 +777,7 @@ await Task.Run(() => } playlist = tempPlaylist.ToArray(); } - //to select a new song for playback even a song was playing when remove was clicked - if (wasPlaying == true) - { - if (allItemsSelected == false && currentlySelectedSong != null && currentlySelectedSong.Length > 0) - { - int i = currentlyPlayingSongIndex; - currentlySelectedSong = (string[])playlist[i].Clone(); - } - } //a problem with this is that the music that will be played might be further in the list or behind the original song that was being - //played. solving this won't take much time --> put aside for later - + //the trimmed and updated playlist array will replace the current playlist if (CurrentPlaylist == Playlists.allSongs) { @@ -954,23 +802,30 @@ await Task.Run(() => } } } + //to select a new song for playback even a song was playing when remove was clicked + if (wasPlaying == true) + { + if (allItemsSelected == false && currentlySelectedSong != null && currentlySelectedSong.Length > 0) + { + if (allItemsSelected) { currentlySelectedSong = null; } + else + { + int i = currentlyPlayingSongIndex; + currentlySelectedSong = (string[])playlist[i].Clone(); + } + } + } //a problem with this is that the music that will be played might be further in the list or behind the original song that was being + //played. solving this won't take much time --> put aside for later + //removing the items, their arts and thumbnails from the data base foreach (string[]? item in toBeRemovedItems) { AudioDataBase.RemoveSongFromDataBase(item, removeFromAllPlaylists, CurrentPlaylistName); } - //for disposal tempPlaylist.Clear(); - tempPlaylist = null; toBeRemovedItems.Clear(); - toBeRemovedItems = null; toBeRemovedItemsIndices.Clear(); - toBeRemovedItemsIndices = null; - playlist = null; - - GC.Collect(); - GC.WaitForPendingFinalizers(); }); } @@ -1062,19 +917,11 @@ await Task.Run(() => isModified = false; //to dispose of - musicDatabase = null; - AllSongs = null; artsToRemove.Clear(); - artsToRemove = null; } - // to dispose of - options = null; - settings = null; } - //to Dispose of existingDatabases.Clear(); - existingDatabases = null; }); //return InvalidsFoundAndRemoved; } @@ -1128,8 +975,6 @@ await Task.Run(() => thumbnail.Save(thumbnailSaver, art.RawFormat); thumbnailSaver.Dispose(); } - art = null; - thumbnail = null; picConverter.Dispose(); } else @@ -1147,18 +992,11 @@ await Task.Run(() => thumbnail.Save(thumbnailSaver, art.RawFormat); thumbnailSaver.Dispose(); } - art = null; - thumbnail = null; } - art = null; musicTags.Dispose(); - tempPics = null; - thumbnail = null; } thumbnail = Image.FromFile(musicData[i][5]); musicThumbnails[i] = (Image)thumbnail.Clone(); - - thumbnail = null; } } return musicThumbnails; @@ -1173,6 +1011,8 @@ await Task.Run(() => /// public static (string[][]?, Image[]?) AddSongsToAudioDataBase(string[]? songPaths, bool readDataBaseFully = false, SortOrders sort = SortOrders.CustomSort) { + if (!System.IO.Directory.Exists(albumArtsPath)) { System.IO.Directory.CreateDirectory(albumArtsPath); } + if (!System.IO.Directory.Exists(albumArtsThumbnailsPath)) { System.IO.Directory.CreateDirectory(albumArtsThumbnailsPath); } string[][]? addedMusicInfoIncomplete = GetAudioFilesInfo(songPaths); if (addedMusicInfoIncomplete != null && addedMusicInfoIncomplete.Length > 0 && addedMusicInfoIncomplete[0] != null) { @@ -1220,7 +1060,6 @@ public static (string[][]?, Image[]?) MainDataBaseIni(SortOrders sort = SortOrde tempMusicDataBase.Load(allMusicDataBase); if (tempMusicDataBase.DocumentElement.ChildNodes.Count > 0) { - tempMusicDataBase = null; AllMusicInfo = ReadAudioDataBase("complete"); if (sort != SortOrders.CustomSort) { AllMusicInfo = PlayList.SortPlaylist(AllMusicInfo, (int)sort); } AllMusicArts = GetMusicThumbnails(AllMusicInfo); diff --git a/Media Player/FilterDuplicates.cs b/Media Player/FilterDuplicates.cs index 3f13f3a..f6f08f6 100644 --- a/Media Player/FilterDuplicates.cs +++ b/Media Player/FilterDuplicates.cs @@ -51,7 +51,6 @@ await Task.Run(() => } allArtFilePaths = (string[])tempArtsPaths.ToArray().Clone(); tempArtsPaths.Clear(); - tempArtsPaths = null; List artsToRemove = new List(); XmlDocument MusicDataBase = new XmlDocument(); @@ -105,8 +104,6 @@ await Task.Run(() => } MusicDataBase.Save(allMusicDataBase); - MusicDataBase = null; - artsToRemove = null; }); GC.Collect(); } diff --git a/Media Player/FormEditor.cs b/Media Player/FormEditor.cs deleted file mode 100644 index 462981b..0000000 --- a/Media Player/FormEditor.cs +++ /dev/null @@ -1,327 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static Khi_Player.SharedFieldsAndVariables; -using System.Xml; - -namespace Khi_Player -{ - /// - /// this class is just here so the codes that are repeated and change the controls or UI - /// can be put into one place in order to have cleaner code and make maintainance easier (written in a functional sort of way) - /// - public class FormEditor - { - public static Form1 CurrentForm; - - /// - /// Need to pass the current form to the constructor once so that methods can function - /// (since they directly edit the controls instead of providing the data) - /// - /// - public FormEditor(Form1 passFormToConstructor) - { - CurrentForm = passFormToConstructor; - } - - /// - /// to enable or disable Darkmode. set to activate DarkMode, and to disable it. - /// - /// - public static void DarkMode(bool EnableDisable) - { - if (EnableDisable) - { - CurrentForm.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.ForeColor = Color.White; - - foreach (Control cont in CurrentForm.Controls) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (Control cont in CurrentForm.musicControlBar.Controls) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (Control cont in CurrentForm.mediaPlayerPanel.Controls) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.playlistToolbar.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.userBar.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.fileToolStripMenuItem.DropDown.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.viewToolStripMenuItem.DropDown.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.listToolStripMenuItem.DropDown.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.rightClickMenu.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - foreach (ToolStripItem cont in CurrentForm.addToPlaylistButton.DropDown.Items) - { - cont.BackColor = Color.FromArgb(41, 41, 41); - cont.ForeColor = Color.White; - } - - CurrentForm.addToPlaylistButton.DropDown.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.addToPlaylistButton.DropDown.ForeColor = Color.White; - CurrentForm.viewToolStripMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.viewToolStripMenuItem.DropDown.ForeColor = Color.White; - CurrentForm.SortListMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.SortListMenuItem.DropDown.ForeColor = Color.White; - CurrentForm.fileToolStripMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.fileToolStripMenuItem.DropDown.ForeColor = Color.White; - CurrentForm.listToolStripMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.listToolStripMenuItem.DropDown.ForeColor = Color.White; - CurrentForm.editPlaylistButton.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.editPlaylistButton.ForeColor = Color.White; - CurrentForm.applyEditStripButton.BackColor = Color.FromArgb(41, 41, 41); - CurrentForm.applyEditStripButton.ForeColor = Color.White; - //for Icons - - CurrentForm.toggleLoop.BackgroundImage = Properties.Resources.Loop_Dark_Mode; - CurrentForm.PlayPause.BackgroundImage = Properties.Resources.Play_Pause__Dark_Mode; - CurrentForm.skip.BackgroundImage = Properties.Resources.Skip_Dark_Mode; - CurrentForm.previous.BackgroundImage = Properties.Resources.Previous_Dark_Mode; - CurrentForm.toggleShuffle.BackgroundImage = Properties.Resources.Shuffle_Dark_Mode; - CurrentForm.stopButton.BackgroundImage = Properties.Resources.Stop_Dark_Mode; - - isDarkMode = true; - } - //turns dark mode to light mode - else - { - CurrentForm.BackColor = Color.White; - CurrentForm.ForeColor = Color.FromArgb(41, 41, 41); - foreach (Control cont in CurrentForm.Controls) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (Control cont in CurrentForm.musicControlBar.Controls) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (Control cont in CurrentForm.mediaPlayerPanel.Controls) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.playlistToolbar.Items) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.userBar.Items) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.fileToolStripMenuItem.DropDown.Items) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.viewToolStripMenuItem.DropDown.Items) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.listToolStripMenuItem.DropDown.Items) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.rightClickMenu.Items) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - foreach (ToolStripItem cont in CurrentForm.addToPlaylistButton.DropDownItems) - { - cont.BackColor = Color.White; - cont.ForeColor = Color.FromArgb(41, 41, 41); - } - CurrentForm.addToPlaylistButton.DropDown.BackColor = Color.White; - CurrentForm.addToPlaylistButton.DropDown.ForeColor = Color.FromArgb(41, 41, 41); - CurrentForm.viewToolStripMenuItem.DropDown.BackColor = Color.White; - CurrentForm.viewToolStripMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); - CurrentForm.SortListMenuItem.DropDown.BackColor = Color.White; - CurrentForm.SortListMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); - CurrentForm.fileToolStripMenuItem.DropDown.BackColor = Color.White; - CurrentForm.fileToolStripMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); - CurrentForm.listToolStripMenuItem.DropDown.BackColor = Color.White; - CurrentForm.listToolStripMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); - CurrentForm.editPlaylistButton.BackColor = Color.White; - CurrentForm.editPlaylistButton.ForeColor = Color.FromArgb(41, 41, 41); - CurrentForm.applyEditStripButton.BackColor = Color.White; - CurrentForm.applyEditStripButton.ForeColor = Color.FromArgb(41, 41, 41); - - //For Icons - CurrentForm.toggleLoop.BackgroundImage = Properties.Resources.loop; - CurrentForm.PlayPause.BackgroundImage = Properties.Resources.Play_Pause; - CurrentForm.skip.BackgroundImage = Properties.Resources.Skip; - CurrentForm.previous.BackgroundImage = Properties.Resources.Previous; - CurrentForm.toggleShuffle.BackgroundImage = Properties.Resources.Shuffle_Light_Mode; - CurrentForm.stopButton.BackgroundImage = Properties.Resources.Stop_Light_Mode; - - isDarkMode = false; - } - } - - /// - /// to create the buttons for the dynamically created playlists - /// - public static void DynamicPlaylistsButtonsIni() - { - List? existingDatabases = new List(); - string[] tempExistingDatabases = System.IO.Directory.GetFiles(applicationPath, "*.xml*", SearchOption.TopDirectoryOnly); - - foreach (string? database in tempExistingDatabases) - { - //just to make sure - if (System.IO.Path.GetExtension(database).ToUpper() == ".XML" && database != allMusicDataBase) - { - existingDatabases.Add(database); - } - } - - if (existingDatabases.Count > 0) - { - // now we have all of the existing xml files in the directory - XmlDocument playlistDatabase; - XmlElement playlistSongs; //the document root node - string? playlistName; - List? playlistNames = new List(); - int i = 1; - foreach (string? playlistPath in existingDatabases) - { - playlistDatabase = new XmlDocument(); - playlistDatabase.Load(playlistPath); - playlistSongs = playlistDatabase.DocumentElement; - playlistName = playlistSongs.GetAttribute("playlist"); - playlistNames.Add(playlistName); - CurrentForm.CreateDynamicPlaylistButton(playlistName); - playlistDatabase = null; - } - } - } - - /// - /// to avoid constantly repeating codes needed for populating musicListView with audio info from the data base - /// - /// - /// - /// - /// - public static void PopulateListView(ref ListView listview, ref string[][]? musicInfos, ref Image[]? musicThumbnails, bool noImageMode = false) - { - if (musicInfos != null && musicInfos.Length > 0) - { - listview.BeginUpdate(); - - if (noImageMode) - { - ListViewItem[] newItems = new ListViewItem[musicInfos.Length]; - int x = 0; - foreach (string[]? music in musicInfos) - { - ListViewItem song = new ListViewItem(music); - song.Name = music[3]; - song.ToolTipText = music[0] + System.Environment.NewLine + music[1] + System.Environment.NewLine + music[2]; - newItems[x] = song; - //listview.Items.Add(song); - //song = null; - } - listview.Items.AddRange(newItems); - } - else - { - int i = 0; - if (listview.LargeImageList == null) - { - listview.LargeImageList = new ImageList - { - ImageSize = new System.Drawing.Size(60, 60) - }; - } - if (listview.LargeImageList.Images.Count > 0) // this is for the listview is gonna be appended and not cleared beforehand - { - i = listview.LargeImageList.Images.Count; - } - - if (musicThumbnails != null) - { - listview.LargeImageList.Images.AddRange(musicThumbnails); - } - else - { - - } - ListViewItem[] newItems = new ListViewItem[musicInfos.Length]; - int x = 0; - foreach (string[]? music in musicInfos) - { - ListViewItem song = new ListViewItem(music, i); - song.Name = music[3]; - song.ToolTipText = music[0] + System.Environment.NewLine + music[1] + System.Environment.NewLine + music[2]; - newItems[x] = song; - //listview.Items.Add(song); - i++; - x++; - //song = null; - } - CurrentForm.musicListView.Items.AddRange(newItems); - //listview.Items.AddRange(newItems); - } - listview.EndUpdate(); - } - } - - /// - /// to prepare the custom source for the search bar - /// - /// - public static async void SearchBarAutoCompleteSource(string[][] playlist) - { - List tempTips = new List(); - await Task.Run(() => - { - foreach (string[]? music in playlist) - { - tempTips.Add(music[0]); - tempTips.Add(music[1]); - tempTips.Add(music[2]); - } - }); - CurrentForm.searchMusicListView.AutoCompleteCustomSource.AddRange(tempTips.ToArray()); - tempTips = null; - } - } -} diff --git a/Media Player/Khi Player Listview Form.cs b/Media Player/Khi Player Listview Form.cs index 19b7a3b..a2e74f5 100644 --- a/Media Player/Khi Player Listview Form.cs +++ b/Media Player/Khi Player Listview Form.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Runtime.InteropServices; using System.Windows; +using System.Xml; using static Khi_Player.AudioDataBase; using static Khi_Player.SharedFieldsAndVariables; @@ -60,7 +61,6 @@ public Form1() break; } } - KhiEditor = new FormEditor(this); if (File.Exists(allMusicDataBase)) { string[][]? info; @@ -69,14 +69,9 @@ public Form1() if (info != null) { allMusicInfo = (string[][]?)info.Clone(); - allMusicArts = (Image[]?)arts.Clone(); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref allMusicArts, false); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + PopulateListView(ref allMusicInfo, ref arts, false); + SearchBarAutoCompleteSource(allMusicInfo); KhiPlayer = new PlayBackFunction(); - - allMusicArts = null; - arts = null; - info = null; } else { @@ -85,17 +80,12 @@ public Form1() (dataBaseInfo, Arts) = FilterDuplicates.TryRepairingDataBase(SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][])dataBaseInfo.Clone(); - Arts = (Image[]?)Arts.Clone(); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref Arts, false); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + PopulateListView(ref allMusicInfo, ref Arts, false); + SearchBarAutoCompleteSource(allMusicInfo); KhiPlayer = new PlayBackFunction(); - - allMusicArts = null; - dataBaseInfo = null; - Arts = null; } } - FormEditor.DynamicPlaylistsButtonsIni(); + DynamicPlaylistsButtonsIni(); } /// @@ -110,8 +100,301 @@ public Form1(string filePath) } PlayBackFunction KhiPlayer; - public static FormEditor KhiEditor; + /// + /// to prepare the custom source for the search bar + /// + /// + public async void SearchBarAutoCompleteSource(string[][] playlist) + { + List tempTips = new List(); + await Task.Run(() => + { + foreach (string[]? music in playlist) + { + tempTips.Add(music[0]); + tempTips.Add(music[1]); + tempTips.Add(music[2]); + } + }); + searchMusicListView.AutoCompleteCustomSource.AddRange(tempTips.ToArray()); + } + + /// + /// to avoid constantly repeating codes needed for populating musicListView with audio info from the data base + /// + /// + /// + /// + public void PopulateListView( ref string[][]? musicInfos, ref Image[]? musicThumbnails, bool noImageMode = false) + { + if (musicInfos != null && musicInfos.Length > 0) + { + musicListView.BeginUpdate(); + + if (noImageMode) + { + ListViewItem[] newItems = new ListViewItem[musicInfos.Length]; + int x = 0; + foreach (string[]? music in musicInfos) + { + ListViewItem song = new ListViewItem(music); + song.Name = music[3]; + song.ToolTipText = music[0] + System.Environment.NewLine + music[1] + System.Environment.NewLine + music[2]; + newItems[x] = song; + } + musicListView.Items.AddRange(newItems); + } + else + { + int i = 0; + if (musicListView.LargeImageList == null) + { + musicListView.LargeImageList = new ImageList + { + ImageSize = new System.Drawing.Size(60, 60) + }; + } + if (musicListView.LargeImageList.Images.Count > 0) // this is for the listview is gonna be appended and not cleared beforehand + { + i = musicListView.LargeImageList.Images.Count; + } + + if (musicThumbnails != null) + { + musicListView.LargeImageList.Images.AddRange(musicThumbnails); + } + else + { + + } + ListViewItem[] newItems = new ListViewItem[musicInfos.Length]; + int x = 0; + foreach (string[]? music in musicInfos) + { + ListViewItem song = new ListViewItem(music, i); + song.Name = music[3]; + song.ToolTipText = music[0] + System.Environment.NewLine + music[1] + System.Environment.NewLine + music[2]; + newItems[x] = song; + i++; + x++; + } + musicListView.Items.AddRange(newItems); + } + musicListView.EndUpdate(); + } + } + + /// + /// to create the buttons for the dynamically created playlists + /// + public void DynamicPlaylistsButtonsIni() + { + List? existingDatabases = new List(); + string[] tempExistingDatabases = System.IO.Directory.GetFiles(applicationPath, "*.xml*", SearchOption.TopDirectoryOnly); + + foreach (string? database in tempExistingDatabases) + { + //just to make sure + if (System.IO.Path.GetExtension(database).ToUpper() == ".XML" && database != allMusicDataBase) + { + existingDatabases.Add(database); + } + } + + if (existingDatabases.Count > 0) + { + // now we have all of the existing xml files in the directory + XmlDocument playlistDatabase; + XmlElement playlistSongs; //the document root node + string? playlistName; + List? playlistNames = new List(); + int i = 1; + foreach (string? playlistPath in existingDatabases) + { + playlistDatabase = new XmlDocument(); + playlistDatabase.Load(playlistPath); + playlistSongs = playlistDatabase.DocumentElement; + playlistName = playlistSongs.GetAttribute("playlist"); + playlistNames.Add(playlistName); + CreateDynamicPlaylistButton(playlistName); + playlistDatabase = null; + } + } + } + + /// + /// to enable or disable Darkmode. set to activate DarkMode, and to disable it. + /// + /// + public void DarkMode(bool EnableDisable) + { + if (EnableDisable) + { + BackColor = Color.FromArgb(41, 41, 41); + ForeColor = Color.White; + + foreach (Control cont in Controls) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (Control cont in musicControlBar.Controls) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (Control cont in mediaPlayerPanel.Controls) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in playlistToolbar.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in userBar.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in fileToolStripMenuItem.DropDown.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in viewToolStripMenuItem.DropDown.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in listToolStripMenuItem.DropDown.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in rightClickMenu.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + foreach (ToolStripItem cont in addToPlaylistButton.DropDown.Items) + { + cont.BackColor = Color.FromArgb(41, 41, 41); + cont.ForeColor = Color.White; + } + + addToPlaylistButton.DropDown.BackColor = Color.FromArgb(41, 41, 41); + addToPlaylistButton.DropDown.ForeColor = Color.White; + viewToolStripMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); + viewToolStripMenuItem.DropDown.ForeColor = Color.White; + SortListMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); + SortListMenuItem.DropDown.ForeColor = Color.White; + fileToolStripMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); + fileToolStripMenuItem.DropDown.ForeColor = Color.White; + listToolStripMenuItem.DropDown.BackColor = Color.FromArgb(41, 41, 41); + listToolStripMenuItem.DropDown.ForeColor = Color.White; + editPlaylistButton.BackColor = Color.FromArgb(41, 41, 41); + editPlaylistButton.ForeColor = Color.White; + applyEditStripButton.BackColor = Color.FromArgb(41, 41, 41); + applyEditStripButton.ForeColor = Color.White; + //for Icons + + toggleLoop.BackgroundImage = Properties.Resources.Loop_Dark_Mode; + PlayPause.BackgroundImage = Properties.Resources.Play_Pause__Dark_Mode; + skip.BackgroundImage = Properties.Resources.Skip_Dark_Mode; + previous.BackgroundImage = Properties.Resources.Previous_Dark_Mode; + toggleShuffle.BackgroundImage = Properties.Resources.Shuffle_Dark_Mode; + stopButton.BackgroundImage = Properties.Resources.Stop_Dark_Mode; + + isDarkMode = true; + } + //turns dark mode to light mode + else + { + BackColor = Color.White; + ForeColor = Color.FromArgb(41, 41, 41); + foreach (Control cont in Controls) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (Control cont in musicControlBar.Controls) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (Control cont in mediaPlayerPanel.Controls) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in playlistToolbar.Items) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in userBar.Items) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in fileToolStripMenuItem.DropDown.Items) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in viewToolStripMenuItem.DropDown.Items) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in listToolStripMenuItem.DropDown.Items) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in rightClickMenu.Items) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + foreach (ToolStripItem cont in addToPlaylistButton.DropDownItems) + { + cont.BackColor = Color.White; + cont.ForeColor = Color.FromArgb(41, 41, 41); + } + addToPlaylistButton.DropDown.BackColor = Color.White; + addToPlaylistButton.DropDown.ForeColor = Color.FromArgb(41, 41, 41); + viewToolStripMenuItem.DropDown.BackColor = Color.White; + viewToolStripMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); + SortListMenuItem.DropDown.BackColor = Color.White; + SortListMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); + fileToolStripMenuItem.DropDown.BackColor = Color.White; + fileToolStripMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); + listToolStripMenuItem.DropDown.BackColor = Color.White; + listToolStripMenuItem.DropDown.ForeColor = Color.FromArgb(41, 41, 41); + editPlaylistButton.BackColor = Color.White; + editPlaylistButton.ForeColor = Color.FromArgb(41, 41, 41); + applyEditStripButton.BackColor = Color.White; + applyEditStripButton.ForeColor = Color.FromArgb(41, 41, 41); + + //For Icons + toggleLoop.BackgroundImage = Properties.Resources.loop; + PlayPause.BackgroundImage = Properties.Resources.Play_Pause; + skip.BackgroundImage = Properties.Resources.Skip; + previous.BackgroundImage = Properties.Resources.Previous; + toggleShuffle.BackgroundImage = Properties.Resources.Shuffle_Light_Mode; + stopButton.BackgroundImage = Properties.Resources.Stop_Light_Mode; + + isDarkMode = false; + } + } + + /// + /// Should call after calling for Playback to update UI related to music infos + /// public void HandlePlaybackUI () { if (seekBar.Enabled == false) { seekBar.Enabled = true; } @@ -272,7 +555,6 @@ public AudioInfo(string audioPath) { //getting a picture of the audio file var tempPics = musicTags.Tag.Pictures; - if (tempPics.Length > 0) { using (MemoryStream picConverter = new MemoryStream(tempPics[0].Data.Data)) @@ -317,31 +599,17 @@ public AudioInfo(string audioPath) path = audioPath; var tempformat = audioPath.Split('.'); - if (tempformat.Length > 1) - { - format = audioPath.Split('.')[tempformat.Length - 1].ToUpper(); - } - else - { - format = audioPath.Split('.')[1].ToUpper(); - } - + if (tempformat.Length > 1) { format = audioPath.Split('.')[tempformat.Length - 1].ToUpper(); } + else { format = audioPath.Split('.')[1].ToUpper(); } if (musicTags.Tag.Title == null) { System.IO.FileInfo sth = new System.IO.FileInfo(path); title = sth.Name; } - else - { - title = musicTags.Tag.Title; - } - + else { title = musicTags.Tag.Title; } //for artists var allArtists = musicTags.Tag.Performers; - if (allArtists.Length == 0) - { - artist = ""; - } + if (allArtists.Length == 0) { artist = ""; } if (allArtists.Length > 1) { System.Windows.Forms.TextBox tempText = new System.Windows.Forms.TextBox(); @@ -353,34 +621,16 @@ public AudioInfo(string audioPath) artist = tempText.Text; tempText.Dispose(); } - else //why? idk - { artist = musicTags.Tag.FirstPerformer; } + else { artist = musicTags.Tag.FirstPerformer; } //For Album - if (musicTags.Tag.Album == null) - { - album = ""; - } - else - { - album = musicTags.Tag.Album; - } - + if (musicTags.Tag.Album == null) { album = ""; } + else { album = musicTags.Tag.Album; } //For genre - if (musicTags.Tag.Genres.Length == 0) - { - genre = ""; - } - else - { - genre = musicTags.Tag.Genres[0]; - } + if (musicTags.Tag.Genres.Length == 0) { genre = ""; } + else { genre = musicTags.Tag.Genres[0]; } var temp = musicTags.Tag.Lyrics; - if (temp != null) - { - lyrics = musicTags.Tag.Lyrics.ReplaceLineEndings(); - } - + if (temp != null) { lyrics = musicTags.Tag.Lyrics.ReplaceLineEndings(); } // all of the infos except art in a string array musicInfo[0] = title; musicInfo[1] = artist; @@ -477,9 +727,7 @@ public static void ScanSystemForAudioFiles() { string[][]? musicsPaths = null; List tempPathsList = new List(); - DriveInfo[] allDrives = DriveInfo.GetDrives(); - List availableDrives = new List(); foreach (DriveInfo drive in allDrives) { @@ -561,24 +809,13 @@ public static void ScanSystemForAudioFiles() } } } - //for disposal - allDirectories = null; } - //for disposal - allDrives = null; - availableDrives = null; string[] addedFiles = tempPathsList.ToArray(); string[][]? AddedMusicInfo; Image[]? AddedMusicArts; (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(addedFiles, true); - allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); - - //for disposal - AddedMusicArts = null; - AddedMusicInfo = null; } /// @@ -659,26 +896,13 @@ await Task.Run(async () => } } } - - //for disposal - allDirectories = null; } - - //for disposal - allDrives = null; - availableDrives = null; }); string[] addedFiles = tempPathsList.ToArray(); string[][]? AddedMusicInfo; Image[]? AddedMusicArts; (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(addedFiles, true, SharedFieldsAndVariables.SortOrder); - allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); - - //for disposal - AddedMusicArts = null; - AddedMusicInfo = null; } //GC.Collect(); } @@ -688,12 +912,12 @@ private void Form1_Load(object sender, EventArgs e) //Dark Mode if (Settings1.Default.DarkMode == true) { - FormEditor.DarkMode(true); + DarkMode(true); isDarkMode = true; } else { - FormEditor.DarkMode(false); + DarkMode(false); isDarkMode = false; } /* // NOT WORKING FOR NOW, DON'T UNCOMMENT OR USE THIS CLASS @@ -705,7 +929,6 @@ private void Form1_Load(object sender, EventArgs e) //for disposal tempImages = null; */ - GC.Collect(); } public void DynamicPlaylistButton_Click(object sender, EventArgs e) @@ -759,24 +982,13 @@ public void DynamicPlaylistButton_Click(object sender, EventArgs e) CurrentPlaylistName = (string)name.Clone(); musicListView.Items.Clear(); musicListView.LargeImageList.Images.Clear(); - FormEditor.PopulateListView(ref musicListView, ref newPlaylistInfo, ref playlistImages); + PopulateListView(ref newPlaylistInfo, ref playlistImages); searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(newPlaylistInfo); + SearchBarAutoCompleteSource(newPlaylistInfo); listUpdated = true; - - //for disposal - newPlaylistInfo = null; - playlistImages = null; - playlistPath = null; - name = null; } - GC.Collect(); - } - else - { - System.Windows.Forms.MessageBox.Show("Please Add songs to this playlist"); } - + else { System.Windows.Forms.MessageBox.Show("Please Add songs to this playlist"); } } public void DynamicAddToPlaylistButton_Click(object sender, EventArgs e) @@ -803,15 +1015,10 @@ public void DynamicAddToPlaylistButton_Click(object sender, EventArgs e) tempPlaylistItems.Add(tempCurrentPlaylist[musicListView.SelectedItems[i].Index]); } } - AudioDataBase.WriteAudioDataBase(tempPlaylistItems.ToArray(), playlistPath, PlaylistName); listUpdated = true; - //for disposal tempPlaylistItems.Clear(); - tempPlaylistItems = null; - PlaylistName = null; - playlistPath = null; } } @@ -832,17 +1039,9 @@ private void renameTextBox_KeyPress(object sender, KeyPressEventArgs e) foreach (char unacceptableChar in unacceptableChars) { - if (renameTextBox.Text.Contains(unacceptableChar)) - { - isAcceptable = false; - } - - } - if (System.IO.File.Exists(applicationPath + renameTextBox.Text + ".xml")) - { - alreadyExists = true; + if (renameTextBox.Text.Contains(unacceptableChar)) { isAcceptable = false; } } - + if (System.IO.File.Exists(applicationPath + renameTextBox.Text + ".xml")) { alreadyExists = true; } //because playlistname will also be used to create the database if (isAcceptable && alreadyExists == false) { @@ -909,22 +1108,13 @@ private void RescanMenuItem_Click(object sender, EventArgs e) song.ToolTipText = music[0] + System.Environment.NewLine + music[1] + System.Environment.NewLine + music[2]; song.Name = music[3]; musicListView.Items.Add(song); - - song = null; x++; } KhiPlayer = new PlayBackFunction(); - - allMusicArts = null; - dataBaseInfo = null; - //artPaths = null; - } - else - { - System.Windows.Forms.MessageBox.Show("Please add music "); } + else { System.Windows.Forms.MessageBox.Show("Please add music "); } + musicListView.EndUpdate(); - GC.Collect(); MemoryManageTimer.Start(); } @@ -940,6 +1130,8 @@ private async void musicListView_DragDrop(object sender, System.Windows.Forms.Dr MemoryManageTimer.Stop(); bool dragDropSuccess = false; int listviewItemsCount = musicListView.Items.Count; + string[][]? AddedMusicInfo; + Image[]? AddedMusicArts = new Image[1]; await Task.Run(() => { @@ -953,41 +1145,25 @@ await Task.Run(() => if (temp == ".mp3" || temp == ".wav" || temp == ".flac" || temp == ".aiff" || temp == ".wma" || temp == ".pcm" || temp == ".aac" || temp == ".oog" || temp == ".alac") { tempPathList.Add(filePath); } } - draggedFiles = tempPathList.ToArray(); - string[][]? AddedMusicInfo; - Image[]? AddedMusicArts; - + draggedFiles = tempPathList.ToArray(); if (listviewItemsCount == 0) { (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(draggedFiles, false, SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); } else { (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(draggedFiles, true, SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); } - //for disposal - tempDraggedFiles = null; tempPathList.Clear(); - tempPathList = null; - draggedFiles = null; - AddedMusicArts = null; - AddedMusicInfo = null; }); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref allMusicArts, false); + PopulateListView(ref allMusicInfo, ref AddedMusicArts, false); searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + SearchBarAutoCompleteSource(allMusicInfo); listUpdated = true; - - //for disposal - allMusicArts = null; - - GC.Collect(); MemoryManageTimer.Start(); } @@ -1151,9 +1327,6 @@ private void musicListView_ItemSelectionChanged(object sender, ListViewItemSelec currentlySelectedSong = Array.Empty(); noSongSelected = true; } - - //for disposal - allInfo = null; } private async void addMusicsToolStripMenuItem_Click(object sender, EventArgs e) @@ -1168,52 +1341,37 @@ private async void addMusicsToolStripMenuItem_Click(object sender, EventArgs e) { string[] tempAddedFilesNames = musicBrowser.FileNames; int index = tempAddedFilesNames.Length; - + string[][]? AddedMusicInfo; + Image[]? AddedMusicArts = new Image[1]; await Task.Run(() => { string[] addedFiles = new string[index]; addedFiles = tempAddedFilesNames; - string[][]? AddedMusicInfo; - Image[]? AddedMusicArts; - if (listviewItemsCount == 0) { (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(addedFiles, false, SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); } else { (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(addedFiles, true, SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); } - //for disposal musicBrowser.Dispose(); - tempAddedFilesNames = null; - addedFiles = null; - AddedMusicArts = null; - AddedMusicInfo = null; }); - - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref allMusicArts, false); + PopulateListView(ref allMusicInfo, ref AddedMusicArts, false); searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + SearchBarAutoCompleteSource(allMusicInfo); listUpdated = true; - - //to dispose - allMusicArts = null; - - GC.Collect(); - GC.WaitForPendingFinalizers(); } MemoryManageTimer.Start(); } private async void addFolderToolStripMenuItem_Click(object sender, EventArgs e) { + Image[]? AddedMusicArts = new Image[1]; MemoryManageTimer.Stop(); int listviewItemsCount = musicListView.Items.Count; OpenFolderDialog musicBrowser = new OpenFolderDialog(); @@ -1249,41 +1407,29 @@ await Task.Run(() => string[] addedFiles = tempPathsList.ToArray(); string[][]? AddedMusicInfo; - Image[]? AddedMusicArts; if (listviewItemsCount == 0) { (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(addedFiles, false ,SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); } else { (AddedMusicInfo, AddedMusicArts) = AudioDataBase.AddSongsToAudioDataBase(addedFiles, true, SharedFieldsAndVariables.SortOrder); allMusicInfo = (string[][]?)AddedMusicInfo.Clone(); - allMusicArts = (Image[]?)AddedMusicArts.Clone(); } //for disposal - musicBrowser = null; - tempAddedFolderNames = null; tempPathsList.Clear(); - tempPathsList = null; - addedFiles = null; - AddedMusicArts = null; - AddedMusicInfo = null; + }); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref allMusicArts, false); + PopulateListView(ref allMusicInfo, ref AddedMusicArts, false); searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + SearchBarAutoCompleteSource(allMusicInfo); listUpdated = true; //to dispose - allMusicArts = null; musicBrowser = null; - tempAddedFolderNames = null; - //tempPathsList.Clear(); - //tempPathsList = null; GC.Collect(); GC.WaitForPendingFinalizers(); @@ -1327,10 +1473,6 @@ private void musicListView_MouseClick(object sender, MouseEventArgs e) showItemInfoButton.Enabled = true; editItemTagsButton.Enabled = true; removeItemButton.Enabled = true; - - //for disposal - playlist = null; - clickedItem = null; } } else @@ -1385,23 +1527,12 @@ private async void removeItemButton_Click(object sender, EventArgs e) songSeekTimer.Stop(); mediaPlayer.Stop(); song.Dispose(); - currentlyPlayingSongPic = null; - if (pictureBox1.Image != null) - { - pictureBox1.Image.Dispose(); - pictureBox1.Image = null; - } - } - else - { - wasPlaying = false; + if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } } + else { wasPlaying = false; } musicListView.BeginUpdate(); //removing the items from the listview itself - if (allItemsSelected == true) - { - musicListView.Items.Clear(); - } + if (allItemsSelected == true) { musicListView.Items.Clear(); } else { foreach (var item in listviewItemsToRemove) @@ -1413,12 +1544,14 @@ private async void removeItemButton_Click(object sender, EventArgs e) //to prevent errors while deleting files GC.Collect(); GC.WaitForPendingFinalizers(); - AudioDataBase.RemoveSongs(toBeRemovedItemsIndices, wasPlaying); + AudioDataBase.RemoveSongs(toBeRemovedItemsIndices, wasPlaying, allItemsSelected); + await Task.Delay(1000); listUpdated = true; + isShuffled = false; - if (wasPlaying == true && allItemsSelected == false) - { - PlayPause_Click(sender, e); + if (wasPlaying == true && allItemsSelected == false && musicListView.Items.Count > 0) + { + PlayPause.PerformClick(); } else if (allItemsSelected == true) { @@ -1437,9 +1570,7 @@ private async void removeItemButton_Click(object sender, EventArgs e) } //for disposal listviewItemsToRemove.Clear(); - listviewItemsToRemove = null; - } - + } } private void musicListView_KeyPress(object sender, KeyPressEventArgs e) @@ -1652,7 +1783,6 @@ private void songSeekTimer_Tick(object sender, EventArgs e) private void seekBar_MouseDown(object sender, MouseEventArgs e) { seekBarValueBeforeMove = seekBar.Value; - //songSeekTimer.Enabled = false; } private void seekBar_MouseUp(object sender, MouseEventArgs e) @@ -1661,10 +1791,8 @@ private void seekBar_MouseUp(object sender, MouseEventArgs e) if (clickedValue <= seekBar.Maximum) { seekBar.Value = clickedValue; - //PlayBackFunction.song.Skip(clickedValue - seekBarValueBeforeMove); song.Skip(clickedValue - timeValue -1); } - //songSeekTimer.Enabled = true; } private void volumeBar_Scroll(object sender, EventArgs e) @@ -1683,13 +1811,13 @@ private void darkModeMenuItem_Click(object sender, EventArgs e) //turns lightmode to dark mode if (!darkModeMenuItem.Checked) { - FormEditor.DarkMode(true); + DarkMode(true); isDarkMode = true; } //turns dark mode to light mode else { - FormEditor.DarkMode(false); + DarkMode(false); isDarkMode = false; } } @@ -1721,48 +1849,55 @@ private void Form1_Resize(object sender, EventArgs e) private async void allSongsPlaylist_Click(object sender, EventArgs e) { - CurrentPlaylist = Playlists.allSongs; - currentPlaylistLabel.Text = "All Songs"; - allMusicInfo = ReadAudioDataBase("complete"); - - if (SharedFieldsAndVariables.SortOrder != SortOrders.CustomSort) + try { - int order = (int)SharedFieldsAndVariables.SortOrder; - int sortColumn; - - switch (order) + if (System.IO.File.Exists(allMusicDataBase)) { - case 0: - //this is sort based on title - sortColumn = 0; - break; - case 1: - //this is sort based on artist - sortColumn = 1; - break; - case 2: - //this is sort based on album - sortColumn = 2; - break; + CurrentPlaylist = Playlists.allSongs; + currentPlaylistLabel.Text = "All Songs"; + allMusicInfo = ReadAudioDataBase("complete"); - default: - //this is sort based on title - sortColumn = 0; - break; - } - PlayList.SortPlaylist(CurrentPlaylist, sortColumn); - } + if (SharedFieldsAndVariables.SortOrder != SortOrders.CustomSort) + { + int order = (int)SharedFieldsAndVariables.SortOrder; + int sortColumn; - Image[]? arts = GetMusicThumbnails(allMusicInfo); - musicListView.Items.Clear(); - musicListView.LargeImageList.Images.Clear(); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref arts); - searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); - listUpdated = true; - isShuffled = false; + switch (order) + { + case 0: + //this is sort based on title + sortColumn = 0; + break; + case 1: + //this is sort based on artist + sortColumn = 1; + break; + case 2: + //this is sort based on album + sortColumn = 2; + break; - GC.Collect(); + default: + //this is sort based on title + sortColumn = 0; + break; + } + PlayList.SortPlaylist(CurrentPlaylist, sortColumn); + } + + Image[]? arts = GetMusicThumbnails(allMusicInfo); + musicListView.Items.Clear(); + musicListView.LargeImageList.Images.Clear(); + PopulateListView(ref allMusicInfo, ref arts); + searchMusicListView.AutoCompleteCustomSource.Clear(); + SearchBarAutoCompleteSource(allMusicInfo); + listUpdated = true; + isShuffled = false; + + GC.Collect(); + } + } + catch { } } @@ -1772,7 +1907,6 @@ private void searchMusicListView_KeyDown(object sender, KeyEventArgs e) if (e.KeyValue == (int)Keys.Enter || e.KeyValue == (int)Keys.Tab) { - //musicListView.Focus(); var previousPlaylist = CurrentPlaylist; List originalIndices = new List(); @@ -1813,21 +1947,15 @@ private void searchMusicListView_KeyDown(object sender, KeyEventArgs e) string[][]? foundItemsArray = foundItems.ToArray(); Image[]? foundItemsThumbnailsArray = foundItemsThumbnails.ToArray(); - FormEditor.PopulateListView(ref musicListView, ref foundItemsArray, ref foundItemsThumbnailsArray); + PopulateListView(ref foundItemsArray, ref foundItemsThumbnailsArray); //for disposal - foundItems = null; - foundItemsThumbnails = null; - foundItemsArray = null; - foundItemsThumbnailsArray = null; + foundItemsThumbnails.Clear(); } listUpdated = true; CurrentPlaylist = Playlists.searchPlaylist; //for disposal - foundItemsIndices = null; - playlist = null; - GC.Collect(); } else @@ -1838,18 +1966,13 @@ private void searchMusicListView_KeyDown(object sender, KeyEventArgs e) Image[]? arts = GetMusicThumbnails(allMusicInfo); musicListView.Items.Clear(); musicListView.LargeImageList.Images.Clear(); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref arts); + PopulateListView(ref allMusicInfo, ref arts); musicListView.Focus(); searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + SearchBarAutoCompleteSource(allMusicInfo); listUpdated = true; CurrentPlaylist = Playlists.allSongs; - - //for disposal - arts = null; - - GC.Collect(); } } else if (e.KeyValue == (int)Keys.Escape) @@ -1858,17 +1981,13 @@ private void searchMusicListView_KeyDown(object sender, KeyEventArgs e) Image[]? arts = GetMusicThumbnails(allMusicInfo); musicListView.Items.Clear(); musicListView.LargeImageList.Images.Clear(); - FormEditor.PopulateListView(ref musicListView, ref allMusicInfo, ref arts); + PopulateListView(ref allMusicInfo, ref arts); musicListView.Focus(); searchMusicListView.AutoCompleteCustomSource.Clear(); - FormEditor.SearchBarAutoCompleteSource(allMusicInfo); + SearchBarAutoCompleteSource(allMusicInfo); listUpdated = true; CurrentPlaylist = Playlists.allSongs; - - //for disposal - arts = null; - GC.Collect(); } } @@ -1886,14 +2005,11 @@ private void sortListArtistMenuItem_Click(object sender, EventArgs e) musicListView.Items.Clear(); musicListView.LargeImageList.Images.Clear(); Image[]? tempAllArts = (Image[]?)AudioDataBase.GetMusicThumbnails(PlayList.GetCurrentPlaylist()).Clone(); - FormEditor.PopulateListView(ref musicListView, ref playlist, ref tempAllArts); + PopulateListView(ref playlist, ref tempAllArts); listUpdated = true; - - //for disposal - tempAllArts = null; if (CurrentPlaylist != Playlists.allSongs && CurrentPlaylist != Playlists.searchPlaylist) { playlist = null; } - } - SharedFieldsAndVariables.SortOrder = SortOrders.ArtistSort; + SharedFieldsAndVariables.SortOrder = SortOrders.ArtistSort; + } } private void sortListTitleMenuItem_Click(object sender, EventArgs e) @@ -1906,14 +2022,11 @@ private void sortListTitleMenuItem_Click(object sender, EventArgs e) musicListView.Items.Clear(); musicListView.LargeImageList.Images.Clear(); Image[]? tempAllArts = (Image[]?)AudioDataBase.GetMusicThumbnails(PlayList.GetCurrentPlaylist()).Clone(); - FormEditor.PopulateListView(ref musicListView, ref playlist, ref tempAllArts); + PopulateListView(ref playlist, ref tempAllArts); listUpdated = true; - - //for disposal - tempAllArts = null; if (CurrentPlaylist != Playlists.allSongs && CurrentPlaylist != Playlists.searchPlaylist) { playlist = null; } - } - SharedFieldsAndVariables.SortOrder = SortOrders.TitleSort; + SharedFieldsAndVariables.SortOrder = SortOrders.TitleSort; + } } private void sortListAlbumMenuItem_Click(object sender, EventArgs e) @@ -1926,14 +2039,11 @@ private void sortListAlbumMenuItem_Click(object sender, EventArgs e) musicListView.Items.Clear(); musicListView.LargeImageList.Images.Clear(); Image[]? tempAllArts = (Image[]?)AudioDataBase.GetMusicThumbnails(PlayList.GetCurrentPlaylist()).Clone(); - FormEditor.PopulateListView(ref musicListView, ref playlist, ref tempAllArts); + PopulateListView(ref playlist, ref tempAllArts); listUpdated = true; - - //for disposal - tempAllArts = null; if (CurrentPlaylist != Playlists.allSongs && CurrentPlaylist != Playlists.searchPlaylist) { playlist = null; } - } - SharedFieldsAndVariables.SortOrder = SortOrders.AlbumSort; + SharedFieldsAndVariables.SortOrder = SortOrders.AlbumSort; + } } private async void toggleShuffle_Click(object sender, EventArgs e) @@ -2022,10 +2132,6 @@ await Task.Run(() => } }); musicListView.EnsureVisible(index); - - //for disposal - tempPlaylist = null; - playlist = null; } private void editLyricsToolStripButton_Click(object sender, EventArgs e) @@ -2038,7 +2144,6 @@ private void editLyricsToolStripButton_Click(object sender, EventArgs e) private void applyEditStripButton_Click(object sender, EventArgs e) { string? lyrics = lyricsTextBox.Text; - long songPositionBackup = song.Position; string[]? playingSongBackup = currentlyPlayingSongInfo; PlayBackFunction.StopMusic(); diff --git a/Media Player/PlayBackFunction.cs b/Media Player/PlayBackFunction.cs index bda9bde..b0f63c8 100644 --- a/Media Player/PlayBackFunction.cs +++ b/Media Player/PlayBackFunction.cs @@ -63,8 +63,6 @@ private static void LoadAndPlayNewSong(string songPath, bool disposePrevious = t { song.Dispose(); mediaPlayer.Dispose(); - currentlyPlayingSongInfo = null; - currentlyPlayingSongPic = null; } } song = new AudioFileReader(songPath); @@ -253,8 +251,6 @@ public static void PreviousMusic() { song.Dispose(); mediaPlayer.Dispose(); - currentlyPlayingSongInfo = null; - currentlyPlayingSongPic = null; System.Windows.Forms.MessageBox.Show("End of Playlist Reached \r\n Enable Loop for unintrupted playback"); Status = States.Finished; selectedMusicsQue = 0; diff --git a/Media Player/PlayList.cs b/Media Player/PlayList.cs index 1189df0..a18fcd7 100644 --- a/Media Player/PlayList.cs +++ b/Media Player/PlayList.cs @@ -109,10 +109,6 @@ public static string[] GetItemInfoUsingIndex(int index) { item = (string[]?)playlist[index].Clone(); } - - //for disposal - playlist = null; - return item; } diff --git a/Media Player/SharedFieldsAndVariables.cs b/Media Player/SharedFieldsAndVariables.cs index 8bd4547..f65f16c 100644 --- a/Media Player/SharedFieldsAndVariables.cs +++ b/Media Player/SharedFieldsAndVariables.cs @@ -7,6 +7,9 @@ namespace Khi_Player { + /// + /// This class is used as a shared pot for the variables, enums, objects, etc that are used throughout the program + /// public class SharedFieldsAndVariables { public enum Playlists { allSongs, searchPlaylist, DynamicPlaylists };