一个简单的基于 DirectShow 的播放器 2(对话框类)(二)
peg|";
strFilter += "Mp3 File (*.mp3)|*.mp3|";
strFilter += "Wave File (*.wav)|*.wav|";
strFilter += "All Files (*.*)|*.*|";
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, this);
if (IDOK == dlgOpen.DoModal())
{
mSourceFile = dlgOpen.GetPathName();
// Rebuild the file playback filter graph
//创建Graph
CreateGraph();
}
}
其中CreateGraph()函数如下所示:
[cpp]
//创建Graph
void CSimplePlayerDlg::CreateGraph(void)
{
//(如果有)销毁Graph
DestroyGraph();
//新建一个核心类
mFilterGraph = new CDXGraph();
if (mFilterGraph->Create())
{
// Render the source clip
mFilterGraph->RenderFile(mSourceFile);
// Set video window and notification window
mFilterGraph->SetDisplayWindow(mVideoWindow.GetSafeHwnd());
mFilterGraph->SetNotifyWindow(this->GetSafeHwnd());
// Show the first frame
mFilterGraph->Pause();
}
}
与CreateGraph()相反的还有一个DestroyGraph()
[cpp]
//(如果有)销毁Graph
void CSimplePlayerDlg::DestroyGraph(void)
{
if (mFilterGraph)
{
// Stop the filter graph first
mFilterGraph->Stop();
mFilterGraph->SetNotifyWindow(NULL);
delete mFilterGraph;
mFilterGraph = NULL;
}
}
OnButtonPlay():播放按钮的响应函数
[cpp]
//播放
void CSimplePlayerDlg::OnButtonPlay()
{
if (mFilterGraph)
{
mFilterGraph->Run();
// Start a timer
if (mSliderTimer == 0)
{
mSliderTimer = SetTimer(SLIDER_TIMER, 100, NULL);
}
}
}
OnButtonPause():暂停按钮的响应函数
[cpp]
void CSimplePlayerDlg::OnButtonPause()
{
if (mFilterGraph)
{
mFilterGraph->Pause();
// Start a timer
if (mSliderTimer == 0)
{
mSliderTimer = SetTimer(SLIDER_TIMER, 100, NULL);
}
}
}
OnButtonStop():停止按钮的响应函数
[cpp]
void CSimplePlayerDlg::OnButtonStop()
{
if (mFilterGraph)
{
mFilterGraph->SetCurrentPosition(0);
mFilterGraph->Stop();
// Stop the timer
if (mSliderTimer)
{
KillTimer(mSliderTimer);
mSliderTimer = 0;
}
}
}
其他的函数不再一一列举,但意思都是一样的。