使用 IWMPPlayerServices::setTaskPane 翻录

[与此页面关联的功能(Windows 媒体播放器 SDK)是旧版功能。 它已被 MediaPlayer 取代。 MediaPlayer 已针对Windows 10和Windows 11进行了优化。 如果可能,Microsoft 强烈建议新代码使用 MediaPlayer 而不是 Windows 媒体播放器 SDK。 如果可能,Microsoft 建议重写使用旧 API 的现有代码以使用新 API。]

注意

本部分介绍 Windows 媒体播放器 9 系列和 Windows 媒体播放器 10 个 ActiveX 控件的功能。 建议将 IWMPCdromRip 接口与更高版本配合使用。 请参阅 IWMPCdromRip 接口

 

可以使用 Windows 媒体播放器 9 系列或更高版本控件将 CD 曲目复制到用户的计算机。 此过程称为 翻录。 为此,必须在远程模式下嵌入Windows 媒体播放器控件。 有关远程模式的详细信息,请参阅远程处理Windows 媒体播放器控件

若要启动翻录过程,请调用 IWMPPlayerServices::setTaskPane,传递 CopyFromCD?bstrTaskPane 参数的 AutoCopy:id 值,其中 id 是要从中复制的 CD 驱动器的索引。 此索引对应于 IWMPCdromCollection 接口或 CdromMediaChange 事件中的 Cdrom 对象的索引。

下面的示例代码是一个函数,用于启动从索引 0 标识的 CD 驱动器中翻录 CD 的过程。 函数使用以下成员变量:

CComPtr<IWMPPlayer4>  m_spPlayer;  // Smart pointer to IWMPPlayer4.

以下代码显示了 函数的主体:

HRESULT CMyApp::StartRip()
{
    CComPtr<IWMPPlayerApplication>  spPlayerApp;
    CComPtr<IWMPPlayerServices>     spPlayerServices;
    CComBSTR bstrParam;  // Contains the parameter string.

    ATLASSERT(m_spPlayer.p);

    HRESULT hr = m_spPlayer->QueryInterface(&spPlayerServices);

    if(SUCCEEDED(hr))
    {
        // Create the parameter string.
        hr = bstrParam.Append(_T("CopyFromCD?AutoCopy:0"));
    }

    if(SUCCEEDED(hr))
    {
        // Rip the CD in drive 0.
        hr = spPlayerServices->setTaskPane(bstrParam);
    }

    return hr;
}

向用户显示状态

从 CD 复制时,可以检索包含复制操作状态信息的字符串。 为此,必须首先通过调用 IWMPCdrom::get_playlist 检索包含表示 CD 曲目的媒体项的播放列表。 与前面的示例一样,以下示例代码适用于 CD 驱动器索引为零。 它将检索到的播放列表存储在以下成员变量中:

CComPtr<IWMPPlaylist>  m_spCDPlaylist;

以下代码显示了 函数的主体:

HRESULT CMyApp::GetCDPlaylist()
{
    HRESULT hr = S_OK;

    // Release any existing playlist instance.
    m_spCDPlaylist.Release();

    CComPtr<IWMPCdromCollection> spCDDrives;
    CComPtr<IWMPCdrom>  spDrive;
    long lCount = 0;  // Count of CD drives.

    ATLASSERT(m_spPlayer);

    hr = m_spPlayer->get_cdromCollection(&spCDDrives);

    // Test to make sure there is at least one drive.
    if(SUCCEEDED(hr))
    {
        hr = spCDDrives->get_count(&lCount);
    }

    if(SUCCEEDED(hr) && lCount < 1)
    {
        MessageBox(_T("No CD drive detected"), 
                   _T("CD Rip Error"), MB_OK);
        hr = NS_E_CD_READ_ERROR;
    }

    if(SUCCEEDED(hr))
    {
        // Retrieve the first drive.
        hr = spCDDrives->item(0, &spDrive);
    }
    
    if(SUCCEEDED(hr))
    {
        // Get the playlist.
        hr = spDrive->get_playlist(&m_spCDPlaylist);
    }
   
    return hr;
}

接下来,处理 IWMPEvents::MediaChange 事件。 当 CD 曲目被撕裂时,会发生此事件。 传递给事件处理程序的 IDispatch 指针指向 CD 轨的 IWMPMedia 接口。通过 IDispatch 调用 QueryInterface 以检索指向 IWMPMedia 的指针。

若要检测正在翻录的 CD 曲目,请通过调用 IWMPMedia::get_isIdentical 将 事件中的 IWMPMedia 指针与 CD 播放列表中的媒体项进行比较。

调用 IWMPMedia::getItemInfo,将字符串“Status”作为项名称传递。 Status 是媒体项在从 CD 中翻录时Windows 媒体播放器设置的临时属性;该属性在库中不可用。

以下示例代码显示了 一个 MediaChange 事件处理程序。

void CMyApp::MediaChange(IDispatch * Item)
{
    // Test whether the CD playlist exists.
    if(!m_spCDPlaylist)
    {
        return;
    }

    // Declare and initialize variables.
    CComPtr<IWMPMedia> spMedia;
    HRESULT hr = S_OK;
    VARIANT_BOOL vbIdentical = VARIANT_FALSE;
    CComBSTR bstrVal;
    CComBSTR bstrName;
    long lCount = 0;  

    // Create the attribute value string.
    hr = bstrName.Append(_T("Status"));

    if(SUCCEEDED(hr))
    { 
        // Retrieve the IWMPMedia pointer from IDispatch.
        hr = Item->QueryInterface(__uuidof(IWMPMedia),(void**)&spMedia);
    }

    if(SUCCEEDED(hr))
    {
        // Get the value of the Status attribute.
        hr = spMedia->getItemInfo(bstrName, &bstrVal);
    }

    if(SUCCEEDED(hr))
    {
        // If the attribute is empty, set a failure code
        // to simply exit the function.
        if(bstrVal.Length() == 0)
        {
            hr = E_PENDING;
        }
    }
      
    if(SUCCEEDED(hr))
    {
        // Retrieve the count of items in the CD playlist.
        hr = m_spCDPlaylist->get_count(&lCount);
    }

    if(lCount < 1)
    {
        // Exit if the playlist is empty.
        hr = E_PENDING;
    }    

    if(SUCCEEDED(hr) && spMedia)
    {
        // Iterate through the playlist.
        // Compare the media item that raised the event
        // to each CD track. This detects which track
        // has a new status.
        for(long i = 0; i < lCount; i++)
        {
            CComPtr<IWMPMedia> spTrack;

            // Retrieve the CD track as a media object.
            hr = m_spCDPlaylist->get_item(i, &spTrack);

            if(SUCCEEDED(hr))
            {
                // Do the comparison.
                hr = spMedia->get_isIdentical(spTrack, &vbIdentical);
            }

            if(SUCCEEDED(hr) && VARIANT_TRUE == vbIdentical)
            {
                 // spTrack represents a track with changed status.
                 // bstrVal contains a status string. For example:
                 // "Ripping (10%)"
                 //
                 // TODO: Retrieve metadata about the track, and then
                 // display the status to the user.
            }
        }
    }

    return;
}

IWMPCdrom 接口

IWMPCdromCollection 接口

IWMPEvents 接口

IWMPMedia 接口

IWMPPlayerServices 接口

IWMPPlaylist 接口

玩家控件指南

使用 IWMPCdromRip 接口进行翻录