C++控制台界面库_实例2: MP3播放器

这篇博客介绍了一个使用C++和MFC创建的简单MP3播放器,通过mciSendString函数来控制音乐播放。代码中包含了播放、停止、设置音量、获取总时长和当前位置等功能,并且提供了防止程序重复运行的检查机制。
摘要由CSDN通过智能技术生成

播放音乐是用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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值