darthwillard
darthwillard

Reputation: 819

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:

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.

Upvotes: 1

Views: 1201

Answers (3)

Kon
Kon

Reputation: 27441

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;

Upvotes: 2

Peter
Peter

Reputation: 27944

The best way to store your data is in a database, there are different types a small free database to choose from. For example sqlite is nice. You can use sql for fast access of of the data (searching, grouping, etc).

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063013

There doesn't seem to be anything there that would influence storage, do I would say "a list of classes", i.e. a List<Track> or similar, with

public class Track {
    public string Path {get;set;}
    ...
    public string Title {get;set;}
    public string ArtworkPath {get;set;}
}

If the volume is high, you might want to look at databases rather than in-memory storage. SQL Server (Express or Compact) for example, are both free. This may allow for more specialised indexing without much effort, plus pre-built persistence.

Upvotes: 1

Related Questions