在中Visual Studio .NET 2003利用DirectShow播放媒体文件(五)

类别:VC语言 点击:0 评论:0 推荐:

浏览、播放、暂停、关闭四按钮的相应事件响应函数如下:

void CPlayWndDlg::OnBnClickedBrowseButton()

{

    CFileDialog dlgFile(TRUE, NULL, NULL,

       OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

       "Movie Files (*.avi;*.mpg;*.mpeg) |\

       *.avi;*.mpg;*.mpeg |\

       Audio Files (*.wav;*mp3;*.mpa;*.mpu;*.au) |\

       *.wav;*.mp3;*.mpa;*.mpu;*.au |\

       Midi Files (*.mid;*.midi;*.rmi) |\

       *.mid;*.midi;*.rmi| | ", this);

 

    if(dlgFile.DoModal() == IDOK)

    {

       m_strMediaFile  = dlgFile.GetPathName();

       GetDlgItem(IDC_MEDIAFILE_EDIT)->SetWindowText(m_strMediaFile);

    }

    else

       return;

 

    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&m_pGraph);

    HRESULT hr = m_pGraph->RenderFile(CA2W(m_strMediaFile), NULL);

    if(FAILED(hr))

    {

       char szMsg[200];

       AMGetErrorText(hr, szMsg, sizeof(szMsg));

       AfxMessageBox(szMsg);

    }

 

    //指定父窗体

    IVideoWindow* pVidWin = NULL;

    m_pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);

    pVidWin->put_Owner((OAHWND)m_hWnd);

    pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);

 

    CRect rc;

    GetDlgItem(IDC_VW_FRAME)->GetWindowRect(rc);

    ScreenToClient(rc);

    pVidWin->SetWindowPosition(rc.left, rc.top, rc.Width(), rc.Height());

 

    // 注意此处Filter Graph Manager的事件以WM_GRAPHNOTIFY发出(用户定义的消息).

    m_pGraph->QueryInterface(IID_IMediaEventEx, (void **)&m_pEvent);

    m_pEvent->SetNotifyWindow((OAHWND)m_hWnd, WM_GRAPHNOTIFY, 0);

 

    // 设置Seeking

    m_pGraph->QueryInterface(IID_IMediaSeeking, (void **)&m_pMediaSeeking);

 

}

 

void CPlayWndDlg::OnBnClickedPlayButton()

{

    Play();

}

 

void CPlayWndDlg::OnBnClickedPauseButton()

{

    m_pMediaControl->Pause();

 

    m_isPlaying = TRUE;

    GetDlgItem(IDC_PLAY_BUTTON)->EnableWindow(TRUE);

    GetDlgItem(IDC_PAUSE_BUTTON)->EnableWindow(FALSE);

}

 

void CPlayWndDlg::OnBnClickedCancel()

{

    // TODO: 在此添加控件通知处理程序代码

    CleanUp();

    OnCancel();

}

8.添加对WM_GRAPHNOTIFY消息,及其响应函数

在PlayWndDlg添加消息ID定义:

#define WM_GRAPHNOTIFY WM_USER + 101

在PlayWndDlg.h中,代码如下:

// 实现

protected:

    HICON m_hIcon;

 

    // 生成的消息映射函数

    virtual BOOL OnInitDialog();

    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

    afx_msg void OnPaint();

    afx_msg HCURSOR OnQueryDragIcon();

    DECLARE_MESSAGE_MAP()

    afx_msg HRESULT OnGraphNotify(WPARAM wParam,LPARAM lParam);

private:

    IGraphBuilder *m_pGraph;

    IMediaControl *m_pMediaControl;

    IMediaEventEx *m_pEvent;

PlayWndDlg.cpp,如下:

    ON_BN_CLICKED(IDC_BROWSE_BUTTON, OnBnClickedBrowseButton)

    ON_BN_CLICKED(IDC_PLAY_BUTTON, OnBnClickedPlayButton)

    ON_BN_CLICKED(IDC_PAUSE_BUTTON, OnBnClickedPauseButton)

    ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)

    ON_MESSAGE(WM_GRAPHNOTIFY, OnGraphNotify)

END_MESSAGE_MAP()

 

实现如下:

HRESULT CPlayWndDlg::OnGraphNotify(WPARAM wParam,LPARAM lParam)

{

    long levCode, lparam1, lparam2;

    HRESULT hr;

    while (hr = m_pEvent->GetEvent(&levCode, &lparam1, &lparam2, 0), SUCCEEDED(hr))

    {

        hr = m_pEvent->FreeEventParams(levCode, lparam1, lparam2);

        if ((EC_COMPLETE == levCode) || (EC_USERABORT == levCode))

        {

            TRACE("End of the media file!!.\n");

            Stop();

            //CleanUp();

            break;

        }

    }

 

    return hr;

}

 有需要者可以与我索取源码

本文地址:http://com.8s8s.com/it/it1023.htm