- // 音频播放类
- // Player.h
- #include <mmsystem.h>
- #include <windows.h>
- #include "assert.h"
- #pragma comment(lib,"Winmm.lib")
- class Player
- {
- public:
- // 函数调用入口
- // 播放、暂停、停止等接口
- void Play(TCHAR *lpszAction);
- // 获取文件路径名入口
- void GetShortName(TCHAR *lpszPathName);
- public:
- Player();
- virtual ~Player();
- // 私有成员变量
- private:
- TCHAR m_ShortName[MAX_PATH];
- TCHAR m_LongName[MAX_PATH];
- TCHAR m_SongLength[30];
- TCHAR m_CurPosition[30];
- TCHAR m_Status[256];//当前状态
- BOOL m_ifPause; //暂停标记
- BOOL m_ifStop;//停止标记
- int m_iLength;//歌曲长,用秒表示的
- int m_iCurPosition;//当前位置
- int m_iVolume;
- // 私有成员函数
- private:
- void ConvertToShortName();
- void PlayerAction(TCHAR *lpszAction);
- };
- // Player.cpp
- #include "Player.h"
- Player::Player()
- {
- BOOL m_ifPause = FALSE; //暂停标记
- BOOL m_ifStop = TRUE;//停止标记
- }
- Player::~Player()
- {
- }
- // 播放、暂停、停止通用接口
- void Player::Play(TCHAR *lpszAction)
- {
- PlayerAction(lpszAction);
- }
- void Player::GetShortName(TCHAR *lpszPathName)
- {
- if (m_ifPause)
- return;
- if (!m_ifStop)
- PlayerAction("stop");
- wsprintf(m_LongName, "%s", lpszPathName);
- GetShortPathName(m_LongName,m_ShortName,sizeof(m_ShortName)/sizeof(TCHAR));
- }
- // 音乐器的操作
- void Player::PlayerAction(TCHAR *lpszAction)
- {
- TCHAR buffer[MAX_PATH];
- TCHAR Commad[MAX_PATH];
- // 初始化数组
- ZeroMemory(buffer, sizeof(buffer));
- ZeroMemory(Commad, sizeof(Commad));
- // 命令
- //assert(m_ShortName);
- wsprintf(Commad,"%s %s",lpszAction,m_ShortName);
- if (mciSendString(Commad, buffer, sizeof(buffer), NULL) != 0)
- MessageBox(NULL, lpszAction, TEXT("错误"), MB_OK | MB_ICONERROR);
- return;
- if (0 == strcmp(lpszAction, "pause"))
- {
- m_ifPause = TRUE;
- }
- else if (0==strcmp(lpszAction, "stop"))
- {
- m_ifStop = TRUE;
- m_ifPause = FALSE;
- }
- else if(0 == strcmp(lpszAction, "play"))
- {
- m_ifStop = FALSE;
- m_ifPause = FALSE;
- }
- }