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

📄 mfc appdlg.cpp

📁 实现了录音,放音功能!在evc4.0下编译功过,wince5.0下能正常录音,放音,暂停录放音!
💻 CPP
字号:
// MFC AppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC App.h"
#include "MFC AppDlg.h"
//#include "Shobjidl.h"
//#include "Aygshell.h"

#include "AMR_Fixed_Codec.h"

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

#define AMR_MAGIC_NUMBER "#!AMR\n"
#define AMR_MAGIC_LEN     6
#define ENCMODE           MR122

/////////////////////////////////////////////////////////////////////////////
// CMFCAppDlg dialog

#define MMRETCHECK(ret) \
if ( ret != MMSYSERR_NOERROR ) { goto Error; } 


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

void CMFCAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCAppDlg)
	DDX_Control(pDX, IDC_BtVol, m_btVol);
	DDX_Control(pDX, IDC_sliderVol, m_slidVol);
	DDX_Control(pDX, IDC_staticWave, m_staticWave);
	DDX_Control(pDX, IDC_btPlay, m_btPlay);
	DDX_Control(pDX, IDC_slidIndex, m_slidIndex);
	DDX_Control(pDX, IDC_btPause, m_btPaus);
	DDX_Control(pDX, IDC_btClos, m_btClos);
	DDX_Control(pDX, IDC_btRec, m_btRec);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMFCAppDlg, CDialog)
	//{{AFX_MSG_MAP(CMFCAppDlg)
	ON_BN_CLICKED(IDC_btRec, OnRec)
	ON_BN_CLICKED(IDC_btClos, OnClosRec)
	ON_BN_CLICKED(IDC_btPause, OnPaus)
	ON_BN_CLICKED(IDC_btPlay, OnbtPlay)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BtVol, OnBtVol)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCAppDlg message handlers

BOOL CMFCAppDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
//	CWnd* w = GetDesktopWindow();
	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	SetWindowPos(this, 0, 25, 240, 294, TRUE);
	
	UpdateWindow();
	// TODO: Add extra initialization here
	

//	m_btRec.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRec), TRUE);
//	m_btClos.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONStop), TRUE);
//	m_btPlay.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONPlay), TRUE);
//	m_btPaus.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONPause), TRUE);

	m_btPaus.EnableWindow(FALSE);
	m_btClos.EnableWindow(FALSE);
	m_btPlay.EnableWindow(TRUE);

	m_slidIndex.SetRange(0, 100, FALSE);
  	m_slidVol.SetRange(0, 100, FALSE);
	m_bRecFlag = FALSE;
	m_bPlayFlag = FALSE;

	return TRUE;  // return TRUE  unless you set the focus to a control
}


UINT RecProc( LPVOID pParam )
{
	MMRESULT mRet;
	BOOL bRet;
	CMFCAppDlg* pDlg;

	pDlg = (CMFCAppDlg*)pParam;
	
	mRet = Recorder().OpenMic();
	MMRETCHECK(mRet);
	
	pDlg->m_nTimer = SetTimer(pDlg->m_hWnd, 1, 200, NULL);
	while (pDlg->m_bRecFlag)
	{
		mRet = Recorder().ToRec();
		MMRETCHECK(mRet);
	}

	mRet = Recorder().ClosMic();
	MMRETCHECK(mRet);
	mRet = Recorder().ClosRecord();
	MMRETCHECK(mRet);
	bRet = CloseHandle(Recorder().m_fh);
	if (!bRet) 
	{
		goto Error;
	}
// 	SetFilePointer(Recorder().m_fh, 0, NULL, FILE_BEGIN);
// 	bRet = WriteFile(Recorder().m_fh, (PCHAR)&m_WaveHeader, 44L, &cbBytsDone, NULL);
//	if (!bRet || cbBytsDone < AMR_MAGIC_LEN)
//	{
//		goto Error;
//	}
	return 1;
	
Error:
	LPVOID lpMsgBuf;
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		0,
		(LPTSTR)&lpMsgBuf,
		0,
		NULL);
	AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION, 0);
	LocalFree(lpMsgBuf);
	
	return 0;
}


void CMFCAppDlg::OnRec()
{
	// TODO: Add your control notification handler code here
	
	DWORD cbBytsDone;
	WAVEFORMATEX wfx;
	MMRESULT mRet;
	BOOL bRet;
	CodecInterfaceType* pAMR_Codec_Interf;

	wfx.cbSize = 0;
	wfx.wFormatTag = WAVE_FORMAT_PCM;
	wfx.nChannels = 1;
	wfx.nSamplesPerSec = 8000;
	wfx.wBitsPerSample = 16;
	wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
	wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

	m_bRecFlag = TRUE;
	mRet = Recorder().InitRecord(wfx);
	MMRETCHECK(mRet);
	pAMR_Codec_Interf = Get_AMR_EncInterface();
	Recorder().SetCodecInterface(*pAMR_Codec_Interf);

	Recorder().m_fh = CreateFile(TEXT("sound.amr"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (NULL == Recorder().m_fh)
	{
		goto Error;
	}

	bRet = WriteFile(Recorder().m_fh, (PCHAR)AMR_MAGIC_NUMBER, AMR_MAGIC_LEN, &cbBytsDone, NULL);
	if (!bRet || cbBytsDone < AMR_MAGIC_LEN)
	{
		goto Error;
	}

	//Record start ~~~
	m_nRecThreadID = AfxBeginThread(RecProc, this, 0, 0, 0, NULL)->m_nThreadID;
	
	m_nTimer = SetTimer(1, 200, NULL);
	m_iPosCount = 0;
	m_btRec.EnableWindow(FALSE);
	m_btPlay.EnableWindow(FALSE);
	m_btClos.EnableWindow(TRUE);
	return;

Error:
	LPVOID lpMsgBuf;
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		0,
		(LPTSTR)&lpMsgBuf,
		0,
		NULL);
	AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION, 0);
	LocalFree(lpMsgBuf);
	
	return;
}

void CMFCAppDlg::OnClosRec()
{
	// TODO: Add your control notification handler code here
	KillTimer(m_nTimer);
	if (m_bRecFlag) 
	{
		m_bRecFlag = FALSE;
		m_slidIndex.SetPos(0);
		
		m_btClos.EnableWindow(FALSE);
		m_btRec.EnableWindow(TRUE);
		m_btPlay.EnableWindow(TRUE);
	}
	else
	{
		/**/
		m_xplayer.xplaying_command(CPC_STOP, 0);
		/**/
		m_bPlayFlag = FALSE;
		m_slidIndex.SetPos(0);
		m_btClos.EnableWindow(FALSE);
		m_btPaus.EnableWindow(FALSE);
		m_btRec.EnableWindow(TRUE);
		m_btPlay.EnableWindow(TRUE);
	}
	return;
}

void CMFCAppDlg::OnPaus()
{
	// TODO: Add your control notification handler code here
	/**/
	m_xplayer.xplaying_command(CPC_PAUSE, 0);
	KillTimer(m_nTimer);
	/**/
	m_btPaus.EnableWindow(FALSE);
	m_btPlay.EnableWindow(TRUE);
	return;
}

void CMFCAppDlg::OnbtPlay()
{
	// TODO: Add your control notification handler code here
	if (m_bPlayFlag) 
	{
		/**/
		m_xplayer.xplaying_command(CPC_PLAY, 0);
		m_nTimer = SetTimer(2, 200, NULL);
		/**/
		m_btPaus.EnableWindow(TRUE);
		m_btClos.EnableWindow(TRUE);
		m_btPlay.EnableWindow(FALSE);
		m_btRec.EnableWindow(FALSE);
	}
	else
	{
		/**/
		SYS_FILE_CONTENT_TYPE flTarget;
		/**/
		
		m_nTimer = SetTimer(2, 200, NULL);
		m_uiTimer2Count = 0;
		m_btPlay.EnableWindow(FALSE);
		m_btRec.EnableWindow(FALSE);
		m_btClos.EnableWindow(TRUE);
		m_btPaus.EnableWindow(TRUE);
		
		/**/
		memset(&flTarget, 0, sizeof(SYS_FILE_CONTENT_TYPE));
		flTarget.ModeInd = SYS_FILE_DISK_FILE_TYPE;
		strncpy(flTarget.filename, "sound.amr", sizeof("sound.amr"));
//		m_xplayer.xplaying_get_volume(&m_uiVolIndex);
//		m_xplayer.xplaying_set_volume(m_uiVolIndex);
		m_slidVol.SetPos(50);
		m_bPlayFlag = TRUE;
		m_xplayer.xplaying_open(&flTarget, NULL);
		m_xplayer.xplaying_get_play_Timelen(&m_uiPlayTime);
		/**/
	}
	return;
}

void CMFCAppDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 1)
	{
//		static const int xCon = 13;
//		static const int yCon = 13;
//		static const int wCon = 240;
//		static const int hCon = 80;
//		CClientDC dc(this);
//		CBitmap Bitmap;
//		CBitmap * pbmOld = NULL;
//		CDC dcMem;
//		dcMem.CreateCompatibleDC(&dc);
//		Bitmap.CreateCompatibleBitmap(&dc,wCon,hCon);
//		pbmOld = dcMem.SelectObject(&Bitmap);
//		dcMem.PatBlt(0,0,wCon,hCon, BLACKNESS);
//		dcMem.MoveTo(0,hCon/2);
//		//
//		// display incomming signal--key idea!
//		//
//		for(int x =0 ; x < wCon; x++) // display Input
//		{
//			dcMem.LineTo(x,(hCon >> 1) - (Recorder().InputBuffer[x]	>> 7));
//		}
//		dc.BitBlt(xCon,yCon,wCon,hCon,&dcMem, 0, 0, SRCCOPY);
//		dcMem.SelectObject(pbmOld);
//		dcMem.DeleteDC();
		
		m_slidIndex.SetPos(m_iPosCount);
		m_iPosCount = (m_iPosCount++) % 101;


	}
	else if (nIDEvent == 2) 
	{
		/**/
		unsigned int uiProg;

		m_uiTimer2Count++;
		uiProg = m_uiTimer2Count * 20000 / m_uiPlayTime;
		if (uiProg < 99) 
		{
			m_slidIndex.SetPos(uiProg);
		}
		else
		{
			
			KillTimer(m_nTimer);
			m_slidIndex.SetPos(0);
			m_xplayer.xplaying_command(CPC_STOP, 0);
			m_bPlayFlag = FALSE;
			m_slidIndex.SetPos(0);
			m_btClos.EnableWindow(FALSE);
			m_btPaus.EnableWindow(FALSE);
			m_btRec.EnableWindow(TRUE);
			m_btPlay.EnableWindow(TRUE);
		}
		/**/
	}
	else
	{		
		CDialog::OnTimer(nIDEvent);
	}
	return;
}

void CMFCAppDlg::OnBtVol() 
{
	// TODO: Add your control notification handler code here
	if (m_slidVol.IsWindowVisible()) 
	{
		m_slidVol.ShowWindow(FALSE);
	}
	else
	{
		m_slidVol.ShowWindow(TRUE);
	}
	return;
}

BOOL CMFCAppDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->hwnd == m_slidVol.m_hWnd) 
	{
		switch(pMsg->message) 
		{
		case WM_LBUTTONUP:
			{
				m_uiVolIndex = (unsigned int)::SendMessage(m_slidVol.m_hWnd, TBM_GETPOS, 0, 0);
			}
			break;
		default:
			break;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CMFCAppDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	MMRESULT mRet;
	BOOL bRet;

	if (m_bRecFlag)
	{
		mRet = Recorder().ClosMic();
		MMRETCHECK(mRet);
		mRet = Recorder().ClosRecord();
		MMRETCHECK(mRet);
		bRet = CloseHandle(Recorder().m_fh);
		if (!bRet) 
		{
			goto Error;
		}
	}
	CDialog::OnClose();
	return;

Error:
	
	LPVOID lpMsgBuf;
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		0,
		(LPTSTR)&lpMsgBuf,
		0,
		NULL);
	AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION, 0);
	LocalFree(lpMsgBuf);
	CDialog::OnClose();
	return;
}

⌨️ 快捷键说明

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