Compartir a través de


Reading Attribute Values

[The feature associated with this page, Windows Media Player SDK, is a legacy feature. It has been superseded by MediaPlayer. MediaPlayer has been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use MediaPlayer instead of Windows Media Player SDK, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

The attributes you can find in the library and in Windows Media files have predefined names. You can write code that retrieves the value of one attribute by passing the name of that attribute to Media.getItemInfo or Media.getItemInfoByType. You can also write code that retrieves the values of all of the attributes in a file or item.

The following C# example retrieves the value of the Title attribute and displays it in a message box. In this example, the Player object was defined as axWMPLib.AxWindowsMediaPlayer Player.

IWMPMedia media;
string strAttribValue = "";

// Initialize the media object
media = Player.currentMedia;

// Retrieve the object's Title attribute
strAttribValue = media.getItemInfo("Title");

// Display the title
if (strAttribValue != "")
{
    MessageBox.Show("Current title: " + strAttribValue);
}

In the call to getItemInfoByType, the second parameter is a string that specifies the language. If you pass an empty string as shown in this example, the method retrieves the value in the default language. For information about the third parameter, see Attributes with Multiple Values.

The following C# example retrieves the values for a given attribute in the current media item. It returns these values as a semicolon-delimited string. Note that for attributes represented as objects, such as WM/Lyrics_Synchronised, WM/Picture, and WM/UserWebURL, the function returns an empty string.

private string getAttributeValues(string strAttrName, IWMPMedia3 media)
{
    string strAttrValue = "";
    int iAttrCount = 0;

    if (media != null)
    {
        // Retrieve the count of values for this attribute
        iAttrCount = media.getAttributeCountByType(strAttrName, "");

        // Retrieve the values
        for (int i = 0; i < iAttrCount; i++)
        {
            strAttrValue += media.getItemInfoByType(strAttrName, "", i);
            strAttrValue += ";";
        }
    }

    // Return the resulting string
    return strAttrValue;
}

The third argument passed to the getItemInfoByType method is the index of a particular attribute in a set of attributes with the same name.

You can use similar code to retrieve attributes that have unique names. In those cases, getAttributeCountByType returns 1. In the example shown earlier, the call to getItemInfoByType would execute only once.

Changing Attribute Values

Media Item Attributes

Media Object

Reading Attribute Values from a CD or DVD