C++播放音频mav文件和mp3文件

更多语音提示资料,阅读:为自己的软件添加语音提示功能

实现了一个类,该类提供播放音频文件功能

获得音频文件路径:

void  PlayAudio::GetDataPath(string path, string& lsFile)
{
	CFileDialog dlg(TRUE,
		_T("报警音频 (*.mp3)|*.mp3"),
		NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING,
		//_T("报警音频 (*.mp3)|*.mp3|报警音频 (*.wav)|*.wav||"),
		_T("报警音频 (*.mp3)|*.mp3||"),
		NULL);
	dlg.m_ofn.lpstrTitle = "选择报警音频文件";
	string strpath = path;
	dlg.m_ofn.lpstrInitialDir = strpath.c_str();
	//显示文件对话框,获得文件名集合
	if (dlg.DoModal() != IDOK)
		return;
	lsFile =StringAbout::toString( dlg.GetPathName());
 
 
}

调用demo:

PLAYAUDIO("采集完成.wav");

类的源代码:

PlayAudio.cpp

#include "stdafx.h"
#include "PlayAudio.h"
#include <mmSystem.h>
#include "MyDirectory.h"
#include "StringAbout.h"

#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")

CPlayAudio *CPlayAudio::m_play = NULL;
CMutex *CPlayAudio::m_mutex = new CMutex();

CPlayAudio::CPlayAudio()
{
	CMyDirectory mydir;
	CString modulePath = mydir.GetCurrentPath();
	m_modulepath = StringAbout::toString(modulePath);
	num = 1;
}


CPlayAudio::~CPlayAudio()
{
	if (m_mutex != NULL)
	{
		delete m_mutex;
		m_mutex = NULL;
	}
	if (m_play != NULL)
	{
		delete m_play;
		m_play = NULL;
	}
}

void  CPlayAudio::SetAudioPath(std::string path)
{ 
	m_audiopath = path; 
}
void  CPlayAudio::OpenAudio(string filename)
{
	
	m_audiopath = m_modulepath + filename;

	bool isExist = PathFileExists((CString)m_audiopath.c_str());
	if (isExist)
	{
		OnPlaySound();
	}
	else
	{
		if (num == 1)
		{
			num++;
			::MessageBox(NULL, L"报警音频文件不存在", L"提示框", MB_OK | MB_ICONERROR);
		}

		return;
	}
}
void  CPlayAudio::OnPlaySound()
{
	int pos = m_audiopath.rfind(".");
	std::string sType = m_audiopath.substr(pos + 1, m_audiopath.length() - pos - 1);
	if (sType == "wav")
	{
		PlaySound((CString)m_audiopath.c_str(), NULL, SND_FILENAME | SND_ASYNC);
	}

	if (sType == "mp3")
	{
		MCI_OPEN_PARMS m_mciOpen;  //打开参数
		MCI_PLAY_PARMS m_mciPlay;  //播放参数
								   //要操作的文件类型
		m_mciOpen.lpstrDeviceType = L"mpegvideo";
		//要操作的文件路径
		m_mciOpen.lpstrElementName = L"F:\\20170405频段扫描黑名单显示与声音提示\\Alarm.mp3";
		//m_mciOpen.lpstrElementName =(CString)m_audiopath.c_str();
		CString name;
		name.Format(L"alarm%d", rand());
		m_mciOpen.lpstrAlias = name;
		MCIERROR mcierror = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT | MCI_OPEN_ALIAS, (DWORD)&m_mciOpen); //打开文件命令
		if (mcierror) {
			TCHAR buf[128] = { 0 };
			mciGetErrorString(mcierror, buf, 128);
			::MessageBox(NULL, (CString)buf, L"错误", MB_OK | MB_ICONERROR);
			mciSendCommand(m_mciOpen.wDeviceID, MCI_CLOSE, NULL, NULL);
		}
		else {
			m_mciPlay.dwCallback = NULL;
			//播放起始位置ms为单位
			m_mciPlay.dwFrom = (DWORD)(12 * 1); 
			mciSendCommand(m_mciOpen.wDeviceID, MCI_PLAY, MCI_NOTIFY | MCI_FROM, (DWORD)(LPVOID)&m_mciPlay);

		}
	}
}

CPlayAudio * CPlayAudio::GetInstance()
{
	if (m_play != NULL)
	{
		return m_play;
	}
	else
	{
		m_mutex->Lock();
		if (m_play == NULL)
		{
			m_play = new CPlayAudio();		 
		}
		m_mutex->Unlock();
		return m_play;
	}

}

PlayAudio.h

#pragma once
#include <string>
#include "afxmt.h"
using namespace std;

#define PLAYAUDIO CPlayAudio::GetInstance()->OpenAudio

class CPlayAudio
{
public:
	CPlayAudio();
	~CPlayAudio();
private:
	std::string m_audiopath;
	std::string m_modulepath;
	int num;
public:
	static CPlayAudio *m_play;
	static CMutex *m_mutex;
protected:
	void  OnPlaySound();
public:
	static CPlayAudio *GetInstance();
	void  OpenAudio(string filename);
	void  SetAudioPath(std::string path);
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值