这是我写的一段代码,看看有什么不对?!反正编译可以,运行也行,但没法播放?

本人正在进行MFC实践课,要求完成一个软件,本人打算利用MFC自带的ActiveMovie控件,做一个简易的播放器,现在只差一个OPEN按钮不知如何添加函数,希望有哪位高手能够帮忙。谢了!
void CMediaPlayerofOurselvesDlg::OnOpen()

{
//设置过滤字符串
CString strFilter = "AVI Files(*.avi)|*.avi|RealMedia Files(*.rm)|*.rm|MP3 Files(*.mp3)|*.mp3|All Files(*.*)|*.*||";

//构造打开对话框对象
CFileDialog fileDlg(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT,strFilter,this);
CActiveMovie3 act;
if(fileDlg.DoModal()==IDOK)
{
CString pathname=fileDlg.GetFileName();
pathname.MakeUpper();
act.SetFileName(pathname);
act.SetAutoStart(true);
act.SetAutoRewind(true);

} // TODO: Add your control notification handler code here

}

青春有我
浏览 74回答 3
3回答

慕标琳琳

void Cplayer::OnChoiceFile( ){ // TODO: Add your command handler code hereCFileDialog fileDialog( TRUE,"*.OCX",NULL,NULL,"|所有文件(*.*)|*.*||");if (fileDialog.DoModal() == IDOK){ //选择文件 Cstringm_FileName=fileDialog.GetPathName();m_list.AddString(m_FileName); //加入到列表框。}}你要明白一点,activemovie没有自带所谓的open函数,只带了一个SetFileName函数.你要播放文件的时候,从mlist里去出m_FileName就可以了~你要实现循环播放.或者随机播放等功能,就看你对list列表的管理了:)补充:你所添的只是一个文件名,需要FULL PATH 也就是我贴的代码里的m_FileName+"//"+(你的)pathname除非你的应用程序和你的播放文件在同一个目录下,不然都需要绝对路径,而不是相对路径:)

沧海一幻觉

先在使用文件对话框的类的类定义文件中加入对象定义CFileDialog m_MyOpenDialog(TRUE,"avi","*.avi")。随后,在需要使用该对话框的地方加入以下语句:m_MyOpenDialog.Domodal();就可以预览所有.avi文件.其调用规则为:函数原型:CFileDialog(BOOL bOpenFileDialog,LPCTSTR lpszDefExt=NULL,LPCTSTR lpszFileName=NULL,DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPR OMPT,LPCTSTR lpszFilter=NULL,CWnd * pParentWnd=NULL);参数说明:bOpenFileDialog:为TRUE或FALSE。TRUE为打开文件;FALSE为保存文件。lpszDefExt :为缺省的扩展名。lpszFileName :为显示在文件名组合框的编辑框的文件名,一般可选NULLdwFlags :为对话框风格,一般为OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,即隐藏只读选项和覆盖已有文件前提示。LpszFilter :为下拉列表枢中显示文件类型。pParentWnd :一般可选NULL。例如:在文件类型列表框的编辑框要显示“可执行文件(*.exe)”,而在它的下拉列表框中列出“视频文件(*.avi)、All Files(*.*)”内容,则变量定义如下:CFileDialog m_MyOpenDialog(TRUE,"exe",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"可执行文件(*.exe) | *.exe|视频文件(*.bat) | *.bat |All Files (*.*) | *.* ||",NULL);注:看看没有*.*后的||会是什么效果.关于:fseekfseek()函数有三个参数第一个是文件的指针第二个是偏移量 也就是指针移动的字符数//这个参数必须是L类型//如果是负数 表示指针反向移动(向文件头的方向)第三个是偏移量的相对开始位置0 表示从文件开始处 偏移 L1 表示从当前位置 偏移 L2 表示从文件末尾 偏移 L //只能是负数了 也就是只能前向移动fseekMoves the file pointer to a specified location.int fseek(FILE *stream,long offset,int origin);ParametersstreamPointer to FILE structure.offsetNumber of bytes from origin.originInitial position.Return ValueIf successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.RemarksThe fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H:SEEK_CURCurrent position of file pointer.SEEK_ENDEnd of file.SEEK_SETBeginning of file.

隔江千里

//// CDXGraph.h//#ifndef __H_CDXGraph__#define __H_CDXGraph__// Filter graph notification to the specified window#define WM_GRAPHNOTIFY (WM_USER+20)#include "Dshow.h"#include "amstream.h"#include "Qedit.h"class CDXGraph{private:IGraphBuilder * mGraph;IMediaControl * mMediaControl;IMediaEventEx * mEvent;IBasicVideo * mBasicVideo;IBasicAudio * mBasicAudio;IVideoWindow * mVideoWindow;IMediaSeeking * mSeeking;DWORD mObjectTableEntry;public:CDXGraph();virtual ~CDXGraph();public:virtual bool Create(void);virtual void Release(void);virtual bool Attach(IGraphBuilder * inGraphBuilder);IGraphBuilder * GetGraph(void); // Not outstanding reference countIMediaEventEx * GetEventHandle(void);bool ConnectFilters(IPin * inOutputPin, IPin * inInputPin, const AM_MEDIA_TYPE * inMediaType = 0);void DisconnectFilters(IPin * inOutputPin);bool SetDisplayWindow(HWND inWindow);bool SetNotifyWindow(HWND inWindow);bool ResizeVideoWindow(long inLeft, long inTop, long inWidth, long inHeight);void HandleEvent(WPARAM inWParam, LPARAM inLParam);bool Run(void); // Control filter graphbool Stop(void);bool Pause(void);bool IsRunning(void); // Filter graph statusbool IsStopped(void);bool IsPaused(void);bool SetFullScreen(BOOL inEnabled);bool GetFullScreen(void);// IMediaSeekingbool GetCurrentPosition(double * outPosition);bool GetStopPosition(double * outPosition);bool SetCurrentPosition(double inPosition);bool SetStartStopPosition(double inStart, double inStop);bool GetDuration(double * outDuration);bool SetPlaybackRate(double inRate);// Attention: range from -10000 to 0, and 0 is FULL_VOLUME.bool SetAudioVolume(long inVolume);long GetAudioVolume(void);// Attention: range from -10000(left) to 10000(right), and 0 is both.bool SetAudioBalance(long inBalance);long GetAudioBalance(void);bool RenderFile(const char * inFile);bool SnapshotBitmap(const char * outFile);private:void AddToObjectTable(void) ;void RemoveFromObjectTable(void);bool QueryInterfaces(void);};#endif // __H_CDXGraph__//// CDXGraph.cpp//#include "stdafx.h"//#include "streams.h"#include "CDXGraph.h"#include "vfw.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif////////////////////////////////////////////////////////////////////////////////CDXGraph::CDXGraph(){mGraph = NULL;mMediaControl = NULL;mEvent = NULL;mBasicVideo = NULL;mBasicAudio = NULL;mVideoWindow = NULL;mSeeking = NULL;mObjectTableEntry = 0;}CDXGraph::~CDXGraph(){Release();}bool CDXGraph::Create(void){CoInitialize(NULL);if (!mGraph){HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&mGraph);if (SUCCEEDED(hr)){#ifdef _DEBUGAddToObjectTable();#endifreturn QueryInterfaces();}mGraph = 0;}return false;}bool CDXGraph::QueryInterfaces(void){if (mGraph){HRESULT hr = NOERROR;hr = mGraph->QueryInterface(IID_IMediaControl, (void **)&mMediaControl);hr = mGraph->QueryInterface(IID_IMediaEventEx, (void **)&mEvent);hr = mGraph->QueryInterface(IID_IBasicVideo, (void **)&mBasicVideo);hr = mGraph->QueryInterface(IID_IBasicAudio, (void **)&mBasicAudio);hr = mGraph->QueryInterface(IID_IVideoWindow, (void **)&mVideoWindow);hr = mGraph->QueryInterface(IID_IMediaSeeking, (void **)&mSeeking);if (mSeeking){mSeeking->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME);}return SUCCEEDED(hr);}return false;}void CDXGraph::Release(void){if (mSeeking){mSeeking->Release();mSeeking = NULL;}if (mMediaControl){mMediaControl->Release();mMediaControl = NULL;}if (mEvent){mEvent->Release();mEvent = NULL;}if (mBasicVideo){mBasicVideo->Release();mBasicVideo = NULL;}if (mBasicAudio){mBasicAudio->Release();mBasicAudio = NULL;}if (mVideoWindow){mVideoWindow->put_Visible(OAFALSE);mVideoWindow->put_MessageDrain((OAHWND)NULL);mVideoWindow->put_Owner(OAHWND(0));mVideoWindow->Release();mVideoWindow = NULL;}#ifdef _DEBUGRemoveFromObjectTable();#endifif (mGraph){mGraph->Release();mGraph = NULL;}CoUninitialize();}bool CDXGraph::Attach(IGraphBuilder * inGraphBuilder){Release();if (inGraphBuilder){inGraphBuilder->AddRef();mGraph = inGraphBuilder;AddToObjectTable();return QueryInterfaces();}return true;}IGraphBuilder * CDXGraph::GetGraph(void){return mGraph;}IMediaEventEx * CDXGraph::GetEventHandle(void){return mEvent;}// Connect filter from the upstream output pin to the downstream input pinbool CDXGraph::ConnectFilters(IPin * inOutputPin, IPin * inInputPin,const AM_MEDIA_TYPE * inMediaType){if (mGraph && inOutputPin && inInputPin){HRESULT hr = mGraph->ConnectDirect(inOutputPin, inInputPin, inMediaType);return SUCCEEDED(hr) ? true : false;}return false;}void CDXGraph::DisconnectFilters(IPin * inOutputPin){if (mGraph && inOutputPin){HRESULT hr = mGraph->Disconnect(inOutputPin);}}bool CDXGraph::SetDisplayWindow(HWND inWindow){if (mVideoWindow){// long lVisible;// mVideoWindow->get_Visible(&lVisible);// Hide the video window firstmVideoWindow->put_Visible(OAFALSE);mVideoWindow->put_Owner((OAHWND)inWindow);RECT windowRect;::GetClientRect(inWindow, &windowRect);mVideoWindow->put_Left(0);mVideoWindow->put_Top(0);mVideoWindow->put_Width(windowRect.right - windowRect.left);mVideoWindow->put_Height(windowRect.bottom - windowRect.top);mVideoWindow->put_WindowStyle(WS_CHILDWS_CLIPCHILDRENWS_CLIPSIBLINGS);mVideoWindow->put_MessageDrain((OAHWND) inWindow);// Restore the video windowif (inWindow != NULL){// mVideoWindow->put_Visible(lVisible);mVideoWindow->put_Visible(OATRUE);}else{mVideoWindow->put_Visible(OAFALSE);}return true;}return false;}bool CDXGraph::ResizeVideoWindow(long inLeft, long inTop, long inWidth, long inHeight){if (mVideoWindow){long lVisible = OATRUE;mVideoWindow->get_Visible(&lVisible);// Hide the video window firstmVideoWindow->put_Visible(OAFALSE);mVideoWindow->put_Left(inLeft);mVideoWindow->put_Top(inTop);mVideoWindow->put_Width(inWidth);mVideoWindow->put_Height(inHeight);// Restore the video windowmVideoWindow->put_Visible(lVisible);return true;}return false;}bool CDXGraph::SetNotifyWindow(HWND inWindow){if (mEvent){mEvent->SetNotifyWindow((OAHWND)inWindow, WM_GRAPHNOTIFY, 0);return true;}return false;}void CDXGraph::HandleEvent(WPARAM inWParam, LPARAM inLParam){if (mEvent){LONG eventCode = 0, eventParam1 = 0, eventParam2 = 0;while (SUCCEEDED(mEvent->GetEvent(&eventCode, &eventParam1, &eventParam2, 0))){mEvent->FreeEventParams(eventCode, eventParam1, eventParam2);switch (eventCode){case EC_COMPLETE:break;case EC_USERABORT:case EC_ERRORABORT:break;default:break;}}}}bool CDXGraph::Run(void){if (mGraph && mMediaControl){if (!IsRunning()){if (SUCCEEDED(mMediaControl->Run())){return true;}}else{return true;}}return false;}bool CDXGraph::Stop(void){if (mGraph && mMediaControl){if (!IsStopped()){if (SUCCEEDED(mMediaControl->Stop())){return true;}}else{return true;}}return false;}bool CDXGraph::Pause(void){if (mGraph && mMediaControl){if (!IsPaused()){if (SUCCEEDED(mMediaControl->Pause())){return true;}}else{return true;}}return false;}bool CDXGraph::IsRunning(void){if (mGraph && mMediaControl){OAFilterState state = State_Stopped;if (SUCCEEDED(mMediaControl->GetState(10, &state))){return state == State_Running;}}return false;}bool CDXGraph::IsStopped(void){if (mGraph && mMediaControl){OAFilterState state = State_Stopped;if (SUCCEEDED(mMediaControl->GetState(10, &state))){return state == State_Stopped;}}return false;}bool CDXGraph::IsPaused(void){if (mGraph && mMediaControl){OAFilterState state = State_Stopped;if (SUCCEEDED(mMediaControl->GetState(10, &state))){return state == State_Paused;}}return false;}bool CDXGraph::SetFullScreen(BOOL inEnabled){if (mVideoWindow){HRESULT hr = mVideoWindow->put_FullScreenMode(inEnabled ? OATRUE : OAFALSE);return SUCCEEDED(hr);}return false;}bool CDXGraph::GetFullScreen(void){if (mVideoWindow){long fullScreenMode = OAFALSE;mVideoWindow->get_FullScreenMode(&fullScreenMode);return (fullScreenMode == OATRUE);}return false;}// IMediaSeeking featuresbool CDXGraph::GetCurrentPosition(double * outPosition){if (mSeeking){__int64 position = 0;if (SUCCEEDED(mSeeking->GetCurrentPosition(&position))){*outPosition = ((double)position) / 10000000.;return true;}}return false;}bool CDXGraph::GetStopPosition(double * outPosition){if (mSeeking){__int64 position = 0;if (SUCCEEDED(mSeeking->GetStopPosition(&position))){*outPosition = ((double)position) / 10000000.;return true;}}return false;}bool CDXGraph::SetCurrentPosition(double inPosition){if (mSeeking){__int64 one = 10000000;__int64 position = (__int64)(one * inPosition);HRESULT hr = mSeeking->SetPositions(&position, AM_SEEKING_AbsolutePositioning AM_SEEKING_SeekToKeyFrame,0, AM_SEEKING_NoPositioning);return SUCCEEDED(hr);}return false;}bool CDXGraph::SetStartStopPosition(double inStart, double inStop){if (mSeeking){__int64 one = 10000000;__int64 startPos = (__int64)(one * inStart);__int64 stopPos = (__int64)(one * inStop);HRESULT hr = mSeeking->SetPositions(&startPos, AM_SEEKING_AbsolutePositioning AM_SEEKING_SeekToKeyFrame,&stopPos, AM_SEEKING_AbsolutePositioning AM_SEEKING_SeekToKeyFrame);return SUCCEEDED(hr);}return false;}bool CDXGraph::GetDuration(double * outDuration){if (mSeeking){__int64 length = 0;if (SUCCEEDED(mSeeking->GetDuration(&length))){*outDuration = ((double)length) / 10000000.;return true;}}return false;}bool CDXGraph::SetPlaybackRate(double inRate){if (mSeeking){if (SUCCEEDED(mSeeking->SetRate(inRate))){return true;}}return false;}// Attention: range from -10000 to 0, and 0 is FULL_VOLUME.bool CDXGraph::SetAudioVolume(long inVolume){if (mBasicAudio){HRESULT hr = mBasicAudio->put_Volume(inVolume);return SUCCEEDED(hr);}return false;}long CDXGraph::GetAudioVolume(void){long volume = 0;if (mBasicAudio){mBasicAudio->get_Volume(&volume);}return volume;}// Attention: range from -10000(left) to 10000(right), and 0 is both.bool CDXGraph::SetAudioBalance(long inBalance){if (mBasicAudio){HRESULT hr = mBasicAudio->put_Balance(inBalance);return SUCCEEDED(hr);}return false;}long CDXGraph::GetAudioBalance(void){long balance = 0;if (mBasicAudio){mBasicAudio->get_Balance(&balance);}return balance;}bool CDXGraph::RenderFile(const char * inFile){if (mGraph){WCHAR szFilePath[MAX_PATH];MultiByteToWideChar(CP_ACP, 0, inFile, -1, szFilePath, MAX_PATH);if (SUCCEEDED(mGraph->RenderFile(szFilePath, NULL))){return true;}}return false;}bool CDXGraph::SnapshotBitmap(const char * outFile){if (mBasicVideo){long bitmapSize = 0;if (SUCCEEDED(mBasicVideo->GetCurrentImage(&bitmapSize, 0))){bool pass = false;unsigned char * buffer = new unsigned char[bitmapSize];if (SUCCEEDED(mBasicVideo->GetCurrentImage(&bitmapSize, (long *)buffer))){BITMAPFILEHEADER hdr;LPBITMAPINFOHEADER lpbi;lpbi = (LPBITMAPINFOHEADER)buffer;int nColors = 1 << lpbi->biBitCount;if (nColors > 256)nColors = 0;hdr.bfType = ((WORD) ('M' << 8) 'B'); //always is "BM"hdr.bfSize = bitmapSize + sizeof( hdr );hdr.bfReserved1 = 0;hdr.bfReserved2 = 0;hdr.bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) + lpbi->biSize +nColors * sizeof(RGBQUAD));CFile bitmapFile(outFile, CFile::modeReadWrite CFile::modeCreate CFile::typeBinary);bitmapFile.Write(&hdr, sizeof(BITMAPFILEHEADER));bitmapFile.Write(buffer, bitmapSize);bitmapFile.Close();pass = true;}delete [] buffer;return pass;}}return false;}//////////////////////// For GraphEdit Dubug purpose /////////////////////////////void CDXGraph::AddToObjectTable(void){IMoniker * pMoniker = 0;IRunningObjectTable * objectTable = 0;if (SUCCEEDED(GetRunningObjectTable(0, &objectTable))){WCHAR wsz[256];wsprintfW(wsz, L"FilterGraph %08p pid %08x", (DWORD_PTR)mGraph, GetCurrentProcessId());HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);if (SUCCEEDED(hr)){hr = objectTable->Register(0, mGraph, pMoniker, &mObjectTableEntry);pMoniker->Release();}objectTable->Release();}}void CDXGraph::RemoveFromObjectTable(void){IRunningObjectTable * objectTable = 0;if (SUCCEEDED(GetRunningObjectTable(0, &objectTable))){objectTable->Revoke(mObjectTableEntry);objectTable->Release();mObjectTableEntry = 0;}}使用例子void CMpegImageDlg::OnOpenButton(){// TODO: Add your control notification handler code hereCString strFilter ="MPEG File (*.mpg;*.mpeg)*.mpg;*.mpeg""AVI File (*.avi)*.avi""Mp3 File (*.mp3)*.mp3""Wave File (*.wav)*.wav""All Files (*.*)*.*";CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST OFN_HIDEREADONLY,strFilter, this);if (IDOK == dlgOpen.DoModal()){CString strSourceFile = dlgOpen.GetPathName();// Rebuild the file playback filter graphCMpegImageDlg::CreateGraph(strSourceFile);GetDlgItem(ID_OPEN_BUTTON)->EnableWindow(FALSE);GetDlgItem(ID_PLAY_BUTTON)->EnableWindow(TRUE);}}void CMpegImageDlg::CreateGraph(const CString& strSourceFile){CMpegImageDlg::DestroyGraph();m_FilterGraph = new CDXGraph();if (m_FilterGraph->Create()){// Render the source clipm_FilterGraph->RenderFile(strSourceFile);// Set video window and notification windowm_FilterGraph->SetDisplayWindow(m_VideoWindow.GetSafeHwnd());m_FilterGraph->SetNotifyWindow(this->GetSafeHwnd());// Show the first framem_FilterGraph->Pause();}}void CMpegImageDlg::DestroyGraph(void){if(m_FilterGraph){// Stop the filter graph firstm_FilterGraph->Stop();m_FilterGraph->SetNotifyWindow(NULL);delete m_FilterGraph;m_FilterGraph = NULL;}}void CMpegImageDlg::OnPlayButton(){// TODO: Add your control notification handler code hereif (m_FilterGraph){GetDlgItem(ID_PLAY_BUTTON)->EnableWindow(FALSE);GetDlgItem(ID_IMAGE_BUTTON)->EnableWindow(TRUE);m_FilterGraph->Run();// Start a timerif (m_SliderTimer == 0){m_SliderTimer = SetTimer(SLIDER_TIMER, 100, NULL);}}}
打开App,查看更多内容
随时随地看视频慕课网APP