Search code examples
c#media-player

How to get name of song instead od title in xml file using getItemInfo() in c#


I am creating an xml file for my all my songs in my songs folder from c#.net.My code is as follows:

 Microsoft.MediaPlayer.Interop.IWMPMedia mediafilesinformation = plist.get_Item(i);
                   mediafilesinformation = obj_wmp.newMedia(filepath);
                    Filename = mediafilesinformation.getItemInfo("SourceURL");
                    Tracknumber = mediafilesinformation.getItemInfo("WM/TrackNumber");
                    Genre = mediafilesinformation.getItemInfo("WM/Genre");
                    Album = mediafilesinformation.getItemInfo("WM/AlbumTitle");
                    Artist = mediafilesinformation.getItemInfo("Artist");
                    Name = mediafilesinformation.getItemInfo("Name");
                    Duration = double.Parse(mediafilesinformation.getItemInfo("Duration"));

I am getting the title of the song in Name,but i have to get the Name of the song.Please suggest me the solution.


Solution

  • The Name attribute in the properties points to FileName if you need that you then you you can use your filepath string.

    string fileName = Path.GetFileName(filepath);
    

    You can also print out all the attributes, debug them and see what values are being held by the attributes. Use Media.getAttributeName method. An example from the same link is below:

    // Store the current media object.
    var cm = Player.currentMedia;
    
    // Get the number of attributes for the current media. 
    var atCount = cm.attributeCount;
    
    // Loop through the attribute list.
    for(var i=0; i < atCount; i++){
    
       // Print each attribute index and name.   
       myText.value += "Attribute " + i +": ";
       myText.value += cm.getAttributeName(i);
       myText.value += "\n";
    }