简易MAP3播放小程序





我通过收集和组合代码,整合了一个简易的小播放器。
  其中还有非常多的不足,还请各位多多指导。

    附件中有源程序以及代码(格式已规范)我通过收集和组合代码,整合了一个简易的小播放器。
其中还有非常多的不足,还请各位多多指导。

MusicPlay.zip

https://pan.baidu.com/s/1O-SI749eBMk20QNzm5h5aw 


#include<windows.h> //NULL、_TEXT、mciSendString、srand()、MultiByteToWideChar()
#pragma comment(lib,"WINMM.LIB") //音乐播放必须库
#pragma comment(lib,"User32.lib") //GetAsyncKeyState()
#include <conio.h> //FILE、kbhit()
#include <cstdio> //printf()
#include <ctime> //clock()、clock_t
#include <direct.h> //use _chdir() and _getcwd() function
#include <io.h> //use _findfirst() and findnext() function
#include <iostream> //wcout、cout、cin
using namespace std;

int songN; //歌曲总数
int currentSong; //当前播放的第n曲子

char path[255];//路径
struct _finddata_t file; //定义结构体变量

long handle;
HANDLE handle2; //不甚了解,句柄类
wchar_t bufN[255];
wchar_t winSelAda[255];

wchar_t* charToWchar(const char *str);
void showMessage(); //显示窗口信息

class WindowsJ {
private:
	RECT rect;
	HWND hwnd;
public:
	WindowsJ() {
		srand((unsigned)time(NULL));
		SetConsoleTitle(L"音乐播放ing");
		moveWindowJ();
	}
	void moveWindowJ() {
		handle2 = GetStdHandle(STD_OUTPUT_HANDLE);
		hwnd = GetForegroundWindow();
		GetWindowRect(hwnd, &rect); //GetWindowRect
		MoveWindow(hwnd, rect.left + 650, rect.top + 220, rect.right - rect.left, rect.bottom - rect.top, true); //移动窗口
	}
	char* wcharToChar(const wchar_t* wc) {
		int len = WideCharToMultiByte(CP_ACP, 0, wc, wcslen(wc), NULL, 0, NULL, NULL);
		char* m_char = new char[len + 1];
		WideCharToMultiByte(CP_ACP, 0, wc, wcslen(wc), m_char, len, NULL, NULL);
		m_char[len] = '\0';
		return m_char;
	}
	bool FileFindJ() {
		system("color fc"); //改变颜色
		_getcwd(path, 255);  //获取当前路径
		sprintf(path, "%s\\music", path);
		_chdir(path);
		FileFindJ2(); //遍历音频文件
		_findclose(handle);
		changeWinWidHei();
		return false;
	}
	void FileFindJ2() {
		handle = _findfirst("*", &file);//查找所有文件
		if (handle == -1)/* 如果handle为-1,表示当前目录为空,则结束查找而返回 */
			return;
		while (!(_findnext(handle, &file))) {
			if (*file.name == '.' || *file.name == '..') {
				songN++;
				continue;
			}
			SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xf2);
			printf("第%d首歌:", songN);
			SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xf5);
			puts(file.name);
			songN++;
		}
	}
	void changeWinWidHei() {
		wsprintf(winSelAda, L"mode con cols=53 lines=%d", songN + 4);
		system(wcharToChar(winSelAda)); //改变宽高mode con cols=51 lines=12
	}
};
wchar_t* charToWchar(const char *str) {
	int length = strlen(str) + 1;
	wchar_t *t = (wchar_t*)malloc(sizeof(wchar_t)*length);
	memset(t, 0, length * sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, 0, str, strlen(str), t, length);
	return t;
}
class MusicJ {
private:
	wchar_t buf[255];
	int musLen;  //音乐文件长度
	clock_t oldt, newt;
	int key; //按键与曲目
	int cnt; //控制正常使用暂停播放
public:
	MusicJ() :key(0), cnt(0) {
		currentSong = rand() % (songN - 1) + 1;
		playmysong(currentSong);  //随机预播
	}
	~MusicJ() {
		mciSendString(TEXT("close all"), NULL, 0, NULL);
	}
	void FileNPlay(int n) {
		n++;
		handle = _findfirst("*", &file);//查找所有文件
		for (int i = 0; i < n; i++) {
			_findnext(handle, &file);
			wsprintf(bufN, L"open \"%s\" alias MySong", charToWchar(file.name));
		}
		_findclose(handle);
	}
	int KeyCharge() {
		if (key = _getch() == VK_ESCAPE)
			return VK_ESCAPE;
		// 获取按键信息           或ASCII码  //GetKeyState(VK_LEFT);判断刚按过了某键
		if  (GetAsyncKeyState(VK_SPACE) & 0x8000)  key = 0x20;
		else if (GetAsyncKeyState(0x30) & 0x8000)  key = 0x30;
		else if (GetAsyncKeyState(0x31) & 0x8000)  key = 0x31;
		else if (GetAsyncKeyState(0x32) & 0x8000)  key = 0x32;
		else if (GetAsyncKeyState(0x33) & 0x8000)  key = 0x33;
	}
	void playmysong(int n) {
		FileNPlay(n);
		mciSendString(bufN, 0, 0, 0);
		mciSendString(L"status MySong length", buf, 255, 0);
		musLen = _wtoi(buf);
		oldt = clock();
		mciSendString(L"play MySong", 0, 0, 0);
		showMessage();
	}
	void closesong() {
		mciSendString(L"close all", 0, 0, 0);
	}
	void run() {
		while (true) {
			while (!_kbhit()) {
				key = _getch();
				switch (key) {
				case VK_SPACE:
					cnt++;
					if (cnt % 2) {
						mciSendString(TEXT("pause MySong"), 0, 0, 0);
					}
					else if (!(cnt % 2)) {
						mciSendString(TEXT("resume MySong"), 0, 0, 0);
					}
					break;
				case 'A':
				case 'a':
					mciSendString(L"close all", 0, 0, 0);
					currentSong--;
					if (currentSong == 0) currentSong = songN - 1;
					playmysong(currentSong);
					break;
				case 'D':
				case 'd':
					mciSendString(L"close all", 0, 0, 0);
					currentSong++;
					currentSong = currentSong % songN;
					if (currentSong == 0) currentSong++;
					playmysong(currentSong);
					break;
				case VK_ESCAPE:
					SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xf0);
					return;
				case 0:
					break;
				default:
					break;
				}
			}
		}
	}
};
void showMessage2(); //声明,仅在下行的showMessage()中使用
void showMessage() {
	songN = 0;
	system("cls");
	SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xfc);
	printf("SPACE:暂停/播放||暂时没有自动下一曲功能,支持mp3、wma\n");
	printf("ESC:退出播放||请勿在music文件夹中加入文件夹\n");
	printf("A/D键:上一曲下一曲\n");
	showMessage2();
	_findclose(handle);
}
void showMessage2() {
	handle = _findfirst("*", &file);//查找所有文件
	while (!(_findnext(handle, &file))) {
		if (*file.name == '.' || *file.name == '..') {
			songN++;
			continue;
		}
		SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xf2);
		printf("第%d首歌:", songN);
		if (songN == currentSong) {
			SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xf5);
			puts(file.name);
		}
		else {
			SetConsoleTextAttribute(handle2, FOREGROUND_INTENSITY | 0xf1);
			puts(file.name);
		}
		songN++;
	}
}
void main(int argc, char* argv[]) {
	WindowsJ winJ;
	if (winJ.FileFindJ()) {
		printf("没有文件");
		return;
	}
	MusicJ musJ; musJ.run();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值