WMPLib:player.mediaCollection.getAll()。count始终为0

时间:2012-03-21 20:01:07

标签: c# .net com windows-media-player wmp

我正在尝试编写从用户的Windows Media Player库中读取每个项目的代码。此代码适用于大多数用户,但对于某些用户,getAll()将在其Windows Media Player库中明显包含数百或数千个项目时返回空列表。

var player = new WindowsMediaPlayer();
var collection = player.mediaCollection;
var list = collection.getAll();
int total = list.count;

我通过向wmp.dll添加COM引用来引用WMPLib命名空间。我的应用程序随附Interop.WMPLib.dll。如何配置某些用户的计算机,使其在其库中运行带有许多歌曲的Windows Media Player,但WMPLib无法正常运行?此外,在所有情况下可靠地读取用户的Windows Media Player库有哪些变通方法?

1 个答案:

答案 0 :(得分:1)

尝试使用此代码段并查看它是否适合您。

public List<MusicEntry> GetMusicLibrary()
{ 
  List<MusicEntry> entries; 
  IWMPPlaylist mediaList = null; 
  IWMPMedia mediaItem; 

  try
  { 
    // get the full audio media list 
    mediaList = media.getByAttribute("MediaType", "Audio"); 
    entries = new List<MusicEntry>(mediaList.count); 

    for (int i = 0; i < mediaList.count; i++) 
    {
      mediaItem = mediaList.get_Item(i);

      // create the new entry and populate its properties
      entry = new MusicEntry() 
      { 
        Title = GetTitle(mediaItem), 
        Album = GetAlbum(mediaItem), 
        Artist = GetArtist(mediaItem), 
        TrackNumber = GetTrackNumber(mediaItem), 
        Rating = GetRating(mediaItem), 
        FileType = GetFileType(mediaItem) 
      }; 

      entries.Add(entry); 
    } 
  } 
  finally 
  { 
    // make sure we clean up as this is COM 
    if (mediaList != null) 
    {
      mediaList.clear(); 
    } 
  } 

  return entries;
}

有关更多信息,请参阅有关代码项目的这篇优秀文章。 http://www.codeproject.com/Articles/36338/Export-Windows-Media-Player-Music-Metadata-to-XML