按同步优先级对播放列表进行排序
以下代码执行简单的播放列表类型。 可以在 枚举同步播放列表的示例代码中查看此函数的使用方式。 该函数采用以下参数:
- pPlaylist。 指向要排序的Windows 媒体播放器播放列表的指针。 播放列表中的媒体项目必须指向其他播放列表,而不是单个数字媒体文件。
- bstrSyncAttribute。 一个 BSTR,包含当前设备 (“Sync01”、“Sync02”等) 同步伙伴关系属性的名称。
- plSelected。 指向接收同步播放列表计数的 长 变量的指针。
该函数检查每个媒体项目的同步合作关系属性, (表示 pPlaylist 指定的播放列表) 的播放列表。 如果该属性具有非零值,代码会将媒体项移动到播放列表的开头,并按优先级顺序插入它。
完成后,该函数返回同步播放列表 (同步播放列表) 具有同步伙伴关系属性的非零值的媒体项计数。
STDMETHODIMP CSyncSettings::SortPlaylist(IWMPPlaylist *pPlaylist, BSTR bstrSyncAttribute, long *plSelected)
{
HRESULT hr = S_OK;
long lSyncItemCount = 0;
ATLASSERT (pPlaylist);
ATLASSERT (plSelected);
// Local copies of the parameters.
CComPtr<IWMPPlaylist> spPlaylist(pPlaylist);
CComBSTR bstrAttribute(bstrSyncAttribute);
ATLASSERT (bstrAttribute.Length());
// Get the count of playlist media items.
long lCount = 0;
spPlaylist->get_count(&lCount);
// Walk the playlist.
for(long i = 0; i < lCount; i++)
{
CComPtr<IWMPMedia> spMedia;
CComBSTR bstrVal;
long lPriority = 0;
hr = spPlaylist->get_item(i, &spMedia);
if(SUCCEEDED(hr) && spMedia)
{
// Get the sync priority value as a string.
hr = spMedia->getItemInfo(bstrAttribute, &bstrVal);
}
if(SUCCEEDED(hr) && spMedia)
{
// Convert sync priority to a long number.
lPriority = _wtol(bstrVal);
}
// Sort the playlist.
// Only move the current item if it has a
// sync priority value.
if(lPriority > 0)
{
lSyncItemCount++;
// Walk down the list and insert the item
// in ascending order.
for(long j = 0; j < lCount; j++)
{
CComPtr<IWMPMedia> spMediaCompare;
CComBSTR bstrValCompare;
long lPriorityTest = 0;
hr = spPlaylist->get_item(j, &spMediaCompare);
if(SUCCEEDED(hr) && spMediaCompare.p)
{
hr = spMediaCompare->getItemInfo(bstrAttribute, &bstrValCompare);
}
if(SUCCEEDED(hr) && spMediaCompare.p)
{
lPriorityTest = _wtol(bstrValCompare);
if(lPriority <= lPriorityTest ||
0 == lPriorityTest)
{
hr = spPlaylist->moveItem(i, j);
break;
}
}
} // for j
} // if(lPriority > 0)
} // for i
// Return the sync item count.
*plSelected = lSyncItemCount;
return hr;
}
相关主题