Skip to main content
 首页 » 编程设计

c#之用 c# 编写 mp3 播放器,使用字典或数组作为元数据

2024年02月23日10del

我正在用 C# 编写一个 mp3 播放器,我想知道如何存储元数据以便快速检索。这是我需要为每个文件存储的数据:

  • 文件位置
  • 文件名
  • 艺术家
  • 专辑
  • 轨道名称
  • 专辑封面

我应该如何存储数据?数组字典?字典的字典?我想用单独的条目填充一个列表框,所以如果我有一个显示艺术家的按钮,它将快速获取艺术家信息,将其放入一个数组中并填充列表框。

请您参考如下方法:

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; 

当然,您可以将其分解为更多 object-oriented design通过使歌曲属性也成为复杂的对象。例如:

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; } 
} 

然后,例如,像这样访问相册属性:

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