⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sounddlg.cpp

📁 这是一个基于c++ 的 音频编程的 一个简单的例子
💻 CPP
字号:
// SoundDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Sound.h"
#include "SoundDlg.h"

#include "MMSystem.h"
#pragma comment(lib, "winmm.lib")


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

/////////////////////////////////////////////////////////////////////////////
// CSoundDlg dialog

CSoundDlg::CSoundDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSoundDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSoundDlg)
	m_strCommand = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSoundDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSoundDlg)
	DDX_Text(pDX, IDC_COMMANDSTRING, m_strCommand);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSoundDlg, CDialog)
	//{{AFX_MSG_MAP(CSoundDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SNDPLAY, OnSndplay)
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_BEEP, OnBeep)
	ON_BN_CLICKED(IDC_MCI_OPEN, OnMciOpen)
	ON_BN_CLICKED(IDC_MCI_PLAY, OnMciPlay)
	ON_BN_CLICKED(IDC_MCI_PAUSE, OnMciPause)
	ON_BN_CLICKED(IDC_MCI_STOP, OnMciStop)
	ON_BN_CLICKED(IDC_MCI_EXECUTE, OnMciExecute)
	//}}AFX_MSG_MAP
	ON_MESSAGE(MM_MCINOTIFY, OnMciNotify)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSoundDlg message handlers

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

	// 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
}

// 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 CSoundDlg::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 CSoundDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSoundDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CSoundDlg::OnSndplay() 
{
	// TODO: Add your control notification handler code here
	sndPlaySound("Login.wav", SND_ASYNC);
}

void CSoundDlg::OnPlay() 
{
	// TODO: Add your control notification handler code here
	PlaySound("Tada.wav", NULL, SND_LOOP|SND_ASYNC);
}

void CSoundDlg::OnBeep() 
{
	// TODO: Add your control notification handler code here
	MessageBeep(MB_OK);
}

void CSoundDlg::OnMciOpen() 
{
	// TODO: Add your control notification handler code here
	static char szFilter[] = "waveaudio file(*.wav)|*.wav||";
	CFileDialog dlg(TRUE, "wav", NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilter);
	if (dlg.DoModal() == IDOK)
	{
		CString filename = dlg.GetPathName();
		if (m_bIsPlay)
			mciSendCommand(m_MCIDeviceID, MCI_CLOSE, 0, NULL);
		
		MCI_OPEN_PARMS mciOpenParam;
		mciOpenParam.lpstrDeviceType = "waveaudio";
		mciOpenParam.lpstrElementName = filename;
		
		DWORD dwError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_ELEMENT, (DWORD)(LPVOID)&mciOpenParam);
		if (dwError == 0)
		{
			m_MCIDeviceID = mciOpenParam.wDeviceID;
			m_bIsPlay = FALSE;
			m_bIsPause = FALSE;
		}
         GetDlgItem(IDC_MCI_PLAY)->EnableWindow(TRUE);
		 GetDlgItem(IDC_MCI_PAUSE)->EnableWindow(TRUE);
		 GetDlgItem(IDC_MCI_STOP)->EnableWindow(TRUE);
	}
}

void CSoundDlg::OnMciPlay() 
{
	// TODO: Add your control notification handler code here
	DWORD dwError;
	if (!m_bIsPlay)
	{
		MCI_PLAY_PARMS mciPlayParam;
		mciPlayParam.dwCallback = (long)GetSafeHwnd();
		mciPlayParam.dwFrom		= 0;
		dwError = mciSendCommand(m_MCIDeviceID, MCI_PLAY, MCI_FROM|MCI_NOTIFY, (DWORD)(LPVOID)&mciPlayParam);
	}
	else if (m_bIsPause)
	{
		dwError = mciSendCommand(m_MCIDeviceID, MCI_RESUME, 0, NULL);
		m_bIsPause = FALSE;
	}
	
	m_bIsPlay = TRUE;
}

void CSoundDlg::OnMciPause() 
{
	// TODO: Add your control notification handler code here
	DWORD dwError;
	if (m_bIsPlay)
	{
		dwError = mciSendCommand(m_MCIDeviceID, MCI_PAUSE, 0, NULL);
		m_bIsPause = TRUE;
	}
	else
	{
		m_bIsPause = FALSE;
	}
}

void CSoundDlg::OnMciStop() 
{
	// TODO: Add your control notification handler code here
	DWORD dwError = mciSendCommand(m_MCIDeviceID, MCI_STOP, MCI_WAIT, NULL);
	mciSendCommand(m_MCIDeviceID, MCI_CLOSE, 0, NULL);
}

void CSoundDlg::OnMciExecute() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	mciSendString(m_strCommand, NULL, 0, NULL);
}

LRESULT CSoundDlg::OnMciNotify(WPARAM wParam, LPARAM lParam)
{
	if (wParam == MCI_NOTIFY_SUCCESSFUL)
	{
		m_bIsPlay	= FALSE;
		m_bIsPause	= FALSE;
		return 0;
	}
	
	return -1;
}

⌨️ 快捷键说明

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