Common.h:
点击查看代码
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<list>
#include<time.h>
#include<map>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
MusicPlay.h:
点击查看代码
#pragma once
#include "Common.h"
class Res;
class MusicPlayer
{
public:
//枚举命令
enum CMD
{
Open,Play,Pause,Resume,Close
};
MusicPlayer();
void menu();//菜单
void keydown();//按键处理
void showMusic();
void playMusic();
void pauseMusic();
void resumeMusic();//继续音乐
void nextMusic();
void preMusic();
void randPlayMusic();
void selectMusic();
protected:
//播放音乐的序号
int m_curIndex;
//存储命令
vector<string> m_cmd;
Res* m_pres;
};
Res.h
点击查看代码
#pragma once
#include "Common.h"
class Res
{
public:
Res();
map<int, string>& res();
void show(int curPlayIndex);
protected:
map<int, string> m_res;
};
MusicPlay.cpp
点击查看代码
#include "MusicPlay.h"
#include "Res.h"
MusicPlayer::MusicPlayer() {
m_pres = new Res;
m_curIndex = 1;//音乐从第一首开始
//命令存储到数组中,便于播放音乐
m_cmd.push_back("open ");
m_cmd.push_back("play ");
m_cmd.push_back("pause ");
m_cmd.push_back("resume ");
m_cmd.push_back("close ");
}
void MusicPlayer::menu()
{
cout << "----------[音乐播放器]------------\n"
"\t0.退出播放器\n"
"\t1.播放音乐\n"
"\t2.暂停\n"
"\t3.继续\n"
"\t4.上一首\n"
"\t5.下一首\n"
"\t6.随机播放\n"
"\t7.选择播放\n"
"-----------------------------------\n";
}
void MusicPlayer::keydown()
{
int userKey = 0; cout << "请输入你的选择:" << endl;
cin >> userKey;
switch (userKey)
{
case 0:
exit(0);
break;
case 1:
playMusic();
break;
case 2:
pauseMusic();
break;
case 3:
resumeMusic();
break;
case 4:
preMusic();
break;
case 5:
nextMusic();
break;
case 6:
randPlayMusic();
break;
case 7:
selectMusic();
break;
}
}
void MusicPlayer::showMusic()
{
m_pres->show(m_curIndex);
}
void MusicPlayer::playMusic()
{
//auto r = m_pres->res();
//r[m_curIndex]
string opencmd = m_cmd[CMD::Open] + m_pres->res()[m_curIndex];
mciSendString(opencmd.c_str(), 0, 0, 0);
string playcmd = m_cmd[CMD::Play] + m_pres->res()[m_curIndex];
mciSendString(playcmd.c_str(), 0, 0, 0);
}
void MusicPlayer::pauseMusic()
{
string pausecmd = m_cmd[CMD::Pause] + m_pres->res()[m_curIndex];
mciSendString(pausecmd.c_str(), 0, 0, 0);
}
void MusicPlayer::resumeMusic()
{
string resumecmd = m_cmd[CMD::Resume] + m_pres->res()[m_curIndex];
mciSendString(resumecmd.c_str(), 0, 0, 0);
}
void MusicPlayer::nextMusic()
{
string closecmd = m_cmd[CMD::Close] + m_pres->res()[m_curIndex];
mciSendString(closecmd.c_str(), 0, 0, 0);
//得到上一首的音乐信号
m_curIndex == m_pres->res().size() ? m_curIndex = 1 : m_curIndex++;
playMusic();
}
void MusicPlayer::preMusic()
{//关闭正在播放音乐
string closecmd = m_cmd[CMD::Close] + m_pres->res()[m_curIndex];
mciSendString(closecmd.c_str(), 0, 0, 0);
//得到上一首的音乐信号
m_curIndex == 1 ? m_curIndex = m_pres->res().size(): m_curIndex--;
playMusic();
}
void MusicPlayer::randPlayMusic()
{
string closecmd = m_cmd[CMD::Close] + m_pres->res()[m_curIndex];
mciSendString(closecmd.c_str(), 0, 0, 0);
//得到上一首的音乐信号
m_curIndex = rand() % m_pres->res().size() + 1;
playMusic();
}
void MusicPlayer::selectMusic()
{
string closecmd = m_cmd[CMD::Close] + m_pres->res()[m_curIndex];
mciSendString(closecmd.c_str(), 0, 0, 0);
cout << "请输入播放音乐的序号";
cin >> m_curIndex;
if (m_curIndex<1 || m_curIndex>m_pres->res().size())
{
cout << "序号无效,无法播放" << endl;
}
else {
playMusic();
}
}
Res.cpp
点击查看代码
#include "Res.h"
Res::Res()
{
m_res[1] = "./res/music1.mp3";
m_res[2] = "./res/music2.mp3";
m_res[3] = "./res/music3.mp3";
m_res[4] = "./res/music4.mp3";
m_res[5] = "./res/music5.mp3";
m_res[6] = "./res/music6.mp3";
m_res[7] = "./res/music7.mp3";
m_res[8] = "./res/music8.mp3";
}
map<int, string>& Res::res()
{
// TODO: 在此处插入 return 语句
return m_res;
}
void Res::show(int curPlayIndex)
{
cout << "歌单:" << endl;
for (auto v : m_res) {
if (v.first == curPlayIndex) {
cout << v.first << "\t" << v.second << "\t" << "<------" << endl;
}
else {
cout << v.first << "\t" << v.second << endl;
}
}
}
源.cpp
点击查看代码
#include "MusicPlay.h"
int main() {
srand((unsigned int)time(nullptr));
//自动推断出指针类型
auto p=new MusicPlayer;
while (true) {
p->menu();
p->showMusic();
p->keydown();
system("cls");
}
/*mciSendString("open ./res/music1.mp3",0,0,0);
mciSendString("play ./res/music1.mp3 wait", 0, 0, 0);
mciSendString("close ./res/music1.mp3 wait", 0, 0, 0);*/
return 0;
}