Search code examples
c#arraysdictionarymp3

writing an mp3 player in c#, use dictionary or array for metadata?


i'm writing an mp3 player in c#, and i'm wondering how to store the metadata for quick retrieval. here's the data i need to store for each individual file:

  • file location
  • file name
  • artist
  • album
  • track title
  • album artwork

how should i store the data? dictionary of arrays? dictionary of dictionaries? i want to populate a listbox with individual entries, so if i have a button that says artist, it will quickly get the artist info, put it in an array and populate the listbox.


Solution

  • How about a generic list?

    // define song object.
    public class Song 
    {
        public string FileLocation { get; set; }
        public string FileName { get; set; }
        public string Artist { get; set; }
        public string Album { get; set; }
        public string TrackTitle { get; set; }
        public string AlbumArtwork { get; set; }
    }
    
    // create list of songs.
    List<Song> songs = new List<Song>();
    
    // add new song to list.
    songs.Add(new Song {
            FileLocation = "/filepath/sade.mp3",
            FileName = "Sade", 
            Artist = "Sade", 
            Album = "Sade", 
            TrackTitle = "Smooth Operator", 
            AlbumArtwork "TBD"
    });
    
    // access first song in list.
    Song song = songs[0];
    
    // access property of song.
    string trackTitle = song.TrackTitle;
    

    Of course you could break this down into an even more object-oriented design by making the song properties complex objects as well. For example:

    public class Album
    {
        public string Name
        public DateTime ReleaseDate
        public string Artwork { get; set; }
    }
    
    public class Artist
    {
        public string Name
        public List<Album> Albums
    }
    
    public class Song 
    {
        public string FileLocation { get; set; }
        public string FileName { get; set; }
        public Artist Artist { get; set; }
        public string TrackTitle { get; set; }
    }
    

    And then, for example, access the album properties like this:

    string firstAlbumName = song.Artist.Albums[0].Name;