dxplaywavdlg.cpp

来自「《Visual C++视频技术方案宝典》配套光盘」· C++ 代码 · 共 341 行

CPP
341
字号
// DXPlayWavDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DXPlayWav.h"
#include "DXPlayWavDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include <dshow.h>
#pragma comment(lib, "Strmiids.lib")

IMediaControl* pMediaControl ;
IMoniker *pMoniker;
IFileSourceFilter   *pFileSource;
IBaseFilter   *pAsyncReader;
IBaseFilter  *pAudioRender;
IBaseFilter *pWaveParser;
IFileSinkFilter2* pSink ;
IGraphBuilder *pGraph;
IMediaEvent   *pEvent;
static const GUID CLSID_WAVEParser =
{0xD51BD5A1,0x7548,0x11cf,{0xA5,0x20,0x00,0x80,0xC7,0x7E,0xF5,0x8A}};


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDXPlayWavDlg dialog

CDXPlayWavDlg::CDXPlayWavDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDXPlayWavDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDXPlayWavDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDXPlayWavDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDXPlayWavDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDXPlayWavDlg, CDialog)
	//{{AFX_MSG_MAP(CDXPlayWavDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADDPATH, OnAddpath)
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDXPlayWavDlg message handlers

BOOL CDXPlayWavDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDXPlayWavDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDXPlayWavDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDXPlayWavDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDXPlayWavDlg::OnAddpath() 
{
	CFileDialog file(true,NULL,NULL,NULL,"文件(*.wav)|*.wav||");
	if(file.DoModal()==IDOK)
	{
		CString strname=file.GetPathName();
		GetDlgItem(IDC_PATH)->SetWindowText(strname);

	}	
}

void CDXPlayWavDlg::OnPlay() 
{
	ICaptureGraphBuilder2 * pBuilder = NULL;
	pGraph = NULL;
	pMediaControl = NULL;
	HRESULT hr=CoCreateInstance(CLSID_CaptureGraphBuilder2,0,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void**)&pBuilder);

	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
	if(CoCreateInstance(CLSID_WAVEParser, 
		NULL, CLSCTX_INPROC_SERVER,IID_IBaseFilter, (void **)&pWaveParser)==S_OK)


	pBuilder->SetFiltergraph(pGraph);

	pGraph->QueryInterface(IID_IMediaControl,(void**)&pMediaControl);

	ICreateDevEnum *pDevEnum = NULL;
	CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, 
		IID_ICreateDevEnum, (void **)&pDevEnum);

	IEnumMoniker *pClassEnum = NULL;
	pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pClassEnum, 0);
	ULONG cFetched;
	while (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK)     
	{
		IPropertyBag *pPropBag;
		pMoniker->BindToStorage(0,0,IID_IPropertyBag,(void**)&pPropBag);
		//获取CLSID
		VARIANT varCatClsid;
		varCatClsid.vt = VT_BSTR;
		pPropBag->Read(L"CLSID", &varCatClsid, 0);
		CLSID clsidCat;
		CLSIDFromString(varCatClsid.bstrVal, &clsidCat);
		//获取FriendlyName名称
		WCHAR *wszCatName;
		TCHAR szCatDesc[255];
		VARIANT varCatName;
		varCatName.vt = VT_BSTR;
		pPropBag->Read(L"FriendlyName", &varCatName, 0);
		wszCatName=varCatName.bstrVal;
		WideCharToMultiByte(GetACP(), 0, wszCatName, -1,
    			szCatDesc,255, NULL, NULL);
	}
	pClassEnum->Release();

	CoCreateInstance(CLSID_AsyncReader, NULL, CLSCTX_ALL, 
                        IID_IBaseFilter, (void **)&pAsyncReader);
	pAsyncReader->QueryInterface(IID_IFileSourceFilter,(void**)&pFileSource);

	CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_ALL, 
                        IID_IBaseFilter, (void **)&pAudioRender);

	CString strFileName;
	GetDlgItem(IDC_PATH)->GetWindowText(strFileName);
	
	AM_MEDIA_TYPE type;
	type.majortype = MEDIATYPE_Stream;
	type.subtype =MEDIASUBTYPE_WAVE;
	type.formattype = FORMAT_None;
	type.bFixedSizeSamples = FALSE;
	type.bTemporalCompression = FALSE;
	type.pUnk = NULL;
	WCHAR lpwszFileName[255];
	TCHAR *lpszFileName;
	lpszFileName=strFileName.GetBuffer(0);
	MultiByteToWideChar(CP_ACP, 0, lpszFileName, -1, (LPWSTR)lpwszFileName, MAX_PATH);
	pFileSource->Load(lpwszFileName,&type);

	pGraph->AddFilter(pAsyncReader,L"AsyncReader");
	pGraph->AddFilter(pAudioRender,L"AudioRender");
	pGraph->AddFilter(pWaveParser,L"WaveParser");

	IEnumPins* pEnumPins;
	PIN_DIRECTION pDir;
	IPin *pSoundIn,*pFileOut;
	IPin *pParserIn,*pParserOut;
	IPin* pTmppin=NULL;
	

	pAsyncReader->EnumPins(&pEnumPins);
	while(pEnumPins->Next(1,&pTmppin,NULL)==S_OK)
	{
		pTmppin->QueryDirection(&pDir);
		if(pDir==PINDIR_OUTPUT)
		{
			pFileOut=pTmppin;
			break;
		}
	}
	pEnumPins=NULL;
	pAudioRender->EnumPins(&pEnumPins);
	while(pEnumPins->Next(1,&pTmppin,NULL)==S_OK)
	{
		pTmppin->QueryDirection(&pDir);
		if(pDir==PINDIR_INPUT)
		{
			pSoundIn=pTmppin;
			break;	
		}
	}
	pEnumPins=NULL;
	pWaveParser->EnumPins(&pEnumPins);
	while(pEnumPins->Next(1,&pTmppin,NULL)==S_OK)
	{
		pTmppin->QueryDirection(&pDir);
		if(pDir==PINDIR_INPUT)
		{
			pParserIn=pTmppin;	
		}
	}
	pGraph->ConnectDirect(pFileOut,pParserIn,NULL);
	pEnumPins=NULL;
	pWaveParser->EnumPins(&pEnumPins);
	while(pEnumPins->Next(1,&pTmppin,NULL)==S_OK)
	{
		pTmppin->QueryDirection(&pDir);
		
		if(pDir==PINDIR_OUTPUT)
		{
			pParserOut=pTmppin;
		}
	}
	pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
	pGraph->ConnectDirect(pParserOut,pSoundIn,NULL);
	pMediaControl->Run();
	GetDlgItem(IDC_PLAY)->EnableWindow(false);
	long evCode;
	pEvent->WaitForCompletion(INFINITE, &evCode);
	GetDlgItem(IDC_PLAY)->EnableWindow(true);
}

BOOL CDXPlayWavDlg::DestroyWindow() 
{
	pMediaControl->Release();
	pEvent->Release();
	pGraph->Release();
	CoUninitialize();
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?