WMPLib: player.mediaCollection.getAll().count is always 0
Asked Answered
T

1

16

I am attempting to write code that reads each item from the user's Windows Media Player library. This code works for the majority of users, but for some users, getAll() will return an empty list when they clearly have hundreds or thousands of items in their Windows Media Player library.

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

I am referencing the WMPLib namespace by adding a COM reference to wmp.dll. My application ships with Interop.WMPLib.dll. How would some users' machines be configured in such a way that they run Windows Media Player with many songs in their library, but WMPLib fails to function correctly? Furthermore, what workarounds exist to reliably read the user's Windows Media Player library in all cases?

Teasel answered 21/3, 2012 at 20:1 Comment(4)
If you happen to have access to a machine to test, what value do you get from Settings.mediaAccessRights?Hueston
WMPLib fails to function correctly?, May I please request you to post the failure ?Gillan
In general Interop libraries are specific to a version of DLL, and if something changes in the underlying COM dll, you will have to have the appropriate Interop DLL. Do you know if the WMP... dll is same and compatible across the versions of all the windows. Some windows like WIndows 7 N series will not by default ship with windows media player. Also it is a good idea to get the configuration from the people who reports failures and try to find the facts from themGillan
Along the same lines as @JoshuaDrake's answer, is it possible that the media collection doesn't belong to the same account which is running the code? Also, it may be worth running your code with elevated permissions on one of the problem machines - is it a UAC thing?Zoe
S
1

Try this snippet and see if it works for you.

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

For more information refer to this excellent article on Code Project. http://www.codeproject.com/Articles/36338/Export-Windows-Media-Player-Music-Metadata-to-XML

Schnorkle answered 6/6, 2012 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.