播放音乐是用mciSendString函数封装的类。
需要MFC支持。
示例代码:
// PlayList.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h”
#include “console.h”
#pragma comment(lib,“console.lib”)
#include <Mmsystem.h>
#pragma comment(lib, “winmm.lib”)
#include <direct.h>
#include <time.h>
extern COORD mouse;
void jiemian0(); // 0
char *SelectFile(HWND hwnd);
char *SelectFolder(HWND hwnd);
class PlayMusic
{
private:
TCHAR fileName[MAX_PATH+100];
TCHAR shortName[MAX_PATH];
TCHAR cmd[MAX_PATH+250];
char buf1[260];
char st[256];
public:
int volume; // 范围: 1~1000
long sumlength,current_position;
char sumlength_ts[7],current_position_ts[7];
PlayMusic(void);
PlayMusic(char mu[]);
~PlayMusic(void);
void OpenEquipment();
void CloseEquipment();
void AssignCurrentMusic(char mu[]);
void Play();
void Stop();
void SetVolume();
void GetSumLength();
void GetCurrentPosition();
void RepeatPlay();
void PlayFrom(long position);
bool End();
};
PlayMusic::PlayMusic(void)
{
OpenEquipment();
sumlength=0;
current_position=0;
strcpy_s(sumlength_ts,7,"00:00");
}
PlayMusic::PlayMusic(char mu[])
{
OpenEquipment();
wsprintf(fileName,"%s",mu);
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
sumlength=0;
current_position=0;
strcpy_s(sumlength_ts,7,"00:00");
}
PlayMusic::~PlayMusic(void)
{
CloseEquipment();
}
void PlayMusic::OpenEquipment()
{
wsprintf(cmd,“open %s”,“waveaudio”);
mciSendString(cmd,buf1,sizeof(buf1),NULL);
}
void PlayMusic::CloseEquipment()
{
wsprintf(cmd,“close %s”,“waveaudio”);
mciSendString(cmd,buf1,sizeof(buf1),NULL);
}
void PlayMusic::AssignCurrentMusic(char mu[])
{
wsprintf(fileName,"%s",mu);
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
}
void PlayMusic::Play()
{
wsprintf(cmd,“play %s”,shortName);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::Stop()
{
wsprintf(cmd,“stop %s”,shortName);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::SetVolume()
{
wsprintf(cmd,“setaudio %s volume to %d”,shortName,volume);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::GetSumLength()
{
wsprintf(cmd,“status %s length”,shortName);
mciSendString(cmd,st,256,NULL);
sumlength=strtol(st, NULL, 10);
int mintue,second;
mintue=(int)(sumlength/1000/60);
second=sumlength/1000%60;
sprintf_s(sumlength_ts,7,"%02d:%02d",mintue,second);
}
void PlayMusic::GetCurrentPosition()
{
wsprintf(cmd,“status %s position”,shortName);
mciSendString(cmd,st,256,NULL);
current_position=strtol(st, NULL, 10);
int mintue,second;
mintue=(int)(current_position/1000/60);
second=current_position/1000%60;
sprintf_s(current_position_ts,7,"%02d:%02d",mintue,second);
}
void PlayMusic::RepeatPlay()
{
wsprintf(cmd,“play %s repeat”,shortName);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::PlayFrom(long position)
{
wsprintf(cmd,“play %s from %ld”,shortName,position);
mciSendString(cmd,"",NULL,NULL);
mciSendString(cmd,"",NULL,NULL);
}
bool PlayMusic::End()
{
if(sumlength==0)return false;
float t1;
GetCurrentPosition();
t1=(float)((sumlength-current_position)/1000.0)