可以说整个Windows的多媒体编程都是以DirectShow为基础,那好,来吧,我们直接看DirectShow的一段基础代码。
bool Mp3::Load(LPCWSTR szFile)
{Cleanup();ready = false;if (SUCCEEDED(CoCreateInstance( CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder,(void **)&this->pigb)))//----------使用com口去获取directShow指针{pigb->QueryInterface(IID_IMediaControl, (void **)&pimc);//--------控制player开关暂停pigb->QueryInterface(IID_IMediaEventEx, (void **)&pimex);pigb->QueryInterface(IID_IBasicAudio, (void**)&piba);//--------控制player音量pigb->QueryInterface(IID_IMediaSeeking, (void**)&pims);//--------控制player位置HRESULT hr = pigb->RenderFile(szFile, NULL);if (SUCCEEDED(hr)){ready = true;if(pims){pims->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME);pims->GetDuration(&duration); // returns 10,000,000 for a second.duration = duration;}}}return ready;
}bool Mp3::Play()
{if (ready&&pimc){HRESULT hr = pimc->Run();return SUCCEEDED(hr);}return false;
}bool Mp3::Pause()
{if (ready&&pimc){HRESULT hr = pimc->Pause();return SUCCEEDED(hr);}return false;
}bool Mp3::Stop()
{if (ready&&pimc){HRESULT hr = pimc->Stop();return SUCCEEDED(hr);}return false;
}bool Mp3::WaitForCompletion(long msTimeout, long* EvCode)
{if (ready&&pimex){HRESULT hr = pimex->WaitForCompletion(msTimeout, EvCode);return *EvCode > 0;}return false;
}bool Mp3::SetVolume(long vol)
{if (ready&&piba){HRESULT hr = piba->put_Volume(vol);return SUCCEEDED(hr);}return false;
}long Mp3::GetVolume()
{if (ready&&piba){long vol = -1;HRESULT hr = piba->get_Volume(&vol);if(SUCCEEDED(hr))return vol;}return -1;
}__int64 Mp3::GetDuration()
{return duration;
}__int64 Mp3::GetCurrentPosition()
{if (ready&&pims){__int64 curpos = -1;HRESULT hr = pims->GetCurrentPosition(&curpos);if(SUCCEEDED(hr))return curpos;}return -1;
}bool Mp3::SetPositions(__int64* pCurrent, __int64* pStop, bool bAbsolutePositioning)
{if (ready&&pims){DWORD flags = 0;if(bAbsolutePositioning)flags = AM_SEEKING_AbsolutePositioning | AM_SEEKING_SeekToKeyFrame;elseflags = AM_SEEKING_RelativePositioning | AM_SEEKING_SeekToKeyFrame;HRESULT hr = pims->SetPositions(pCurrent, flags, pStop, flags);if(SUCCEEDED(hr))return true;}return false;
}
看完你应该会知道它是用了播放MP3的。这是一个微软网站上下载的DEMO,原理真的很简单,也就几行代码,几乎不用看注释就知道每个函数干什么用的。
DEMO里面用了C#作为界面,你也可以体会一下c#/c++是如何配合开发的。
源码:Here
如果你在源码天空里面找到很漂亮很商业化的MP3播放器,也可以下载来学习。不过,本人对MP3播放器没什么兴趣。