本次的实训的mp3播放器主要涉及到的内容如下:
MCI API :负责音乐的播放,暂停,继续,停止等功能
https://blog.csdn.net/clearver/article/details/5209244
Easyx 图形库:负责进行一些简单绘画参考https://blog.csdn.net/sandalphon4869/article/details/80862023
文件:http://c.biancheng.net/view/2073.html
https://blog.csdn.net/baidu_36649389/article/details/77370475?utm_source=blogxgwz8
https://www.runoob.com/cprogramming/c-function-fgets.html
https://blog.csdn.net/l59565455/article/details/88085015
https://blog.csdn.net/wangqingchuan92/article/details/77979669
https://blog.csdn.net/wangqingchuan92/article/details/77979669
https://www.cnblogs.com/zhoug2020/p/13527290.html
https://www.cnblogs.com/sky-heaven/p/6830404.html
https://blog.csdn.net/weiyuefei/article/details/70310037
数据结构:
链表,二维数组
注意事项:
1)相关的头文件的引入
#include mci api
#include 文件查找 struct _finddata_t fileInfo
#include _kbhit() 函数 监听用户输入,一旦有输入就返回一个非0,没有输入就是返回0
2)链表的构建
typedef struct music {
int num;
char name[100];
struct music* next;
}*linkMusic, musicNode;
//typedef表示为结构体取别名,加了*就是指针,musicNode可以直接用于结构体定义
#ifndef _MYFUNC_
#define _MYFUNC_
void showTime(struct music* head, struct music* pT);//获取歌曲时间
boolean face();//是否调用,提示信息
#endif
//避免头文件重复引用的问题,头文件中最好将所有要用的函数先声明在.h文件中。实现写在.cpp或者.c中
3)文件读取问题
文件存储有很多种方式,将数据写入文件时可以以空格或者回车为结束。
在读取时,不管是硬fscanf还是gets()进行读取,都要注意一点,以gets为例,gets是读到回车符号结尾,但是回车符号会被读取进去。也就是说,需要将读取到的字符串进行处理,最后一个字符要置为‘0’;一定要注意这个问题。
还有一个_finddata这个结构体的问题:
win10下编译出错:0x00007FFD53320B1D (ntdll.dll)
此外由于在win10系统下不知道是我个人原因还是大部分人都有的的问题在编译时findnext会报错, 因为_findnext()返回类型为intptr_t而非long型,从“intptr_t”转换到“long”丢失了数据。只需要把一般写的:
long HANDLE
改为
intptr_t HANDLE
既可以运行
long _findfirst( char *filespec, struct _finddata_t fileinfo );
返回值:
如果查找成功的话,将返回一个long型的唯一的查找用的句柄(就是一个唯一编号)。这个句柄将在_findnext函数中被使用。若失败,则返回-1。
参数:
filespec:标明文件的字符串,可支持通配符。比如:.c,则表示当前文件夹下的所有后缀为C的文件。
fileinfo :这里就是用来存放文件信息的结构体的指针。这个结构体必须在调用此函数前声明,不过不用初始化,只要分配了内存空间就可以了。
函数成功后,函数会把找到的文件的信息放入这个结构体中。
int _findnext( long handle, struct _finddata_t *fileinfo );
返回值:
若成功返回0,否则返回-1。
参数:
handle:即由_findfirst函数返回回来的句柄。
fileinfo:文件信息结构体的指针。找到文件后,函数将该文件信息放入此结构体中。
int _findclose( long handle );
返回值:成功返回0,失败返回-1。
参数: handle :_findfirst函数返回回来的句柄
细节代码
char ADDR[100] = "C:\\Users\\cg123\\Desktop\\0818四川普通文理平行1征\\0818四川普通文理平行1征\\照片\\*.*";//可以查询路径下的所有类型的文件
memset(newP, 0, strlen(newP));//清空字符数组的所有内容
strncpy(temp, FileInfo.name, 14);//将字符串前14个字符进行复制
strcat(temp, type);//拼接后缀
sprintf(newP, "%s%s", ADDR, temp);
rename(oldP, newP);//去旧地址的位置找到文件,然后将名字改为新地址的名字,如果新旧地址不一样,则表现为移动文件的位置
一下为一个简化的版本主要是文件操作:
#include
#include
#include
#include
typedef struct music {
int num;
char name[100];
struct music* next;
}*linkMusic, musicNode;
int main() {
printf("************************************mp3播放器************************************\n");
printf("输入1 播放音乐;\n");
printf("输入2 暂停音乐;\n");
printf("输入3 继续音乐;\n");
printf("输入4 停止音乐;\n");
printf("输入9 退出程序;\n");
while (1)
{
int num = getch() - 48;
if (num == 1)
{
printf("输入1 播放音乐;\n");
//c:music
//char name[100] ;
char fileName[100] = "C:\\music\\*.mp3";
linkMusic head, p, q;
head = (linkMusic)malloc(sizeof(musicNode));
q = head;
int i = 1;
struct _finddata_t fileInfo;
intptr_t handle;//必须要改
handle = _findfirst(fileName, &fileInfo);
if (handle==-1)
{
printf("该文件夹下没有MP3文件");
}
else
{
p = (linkMusic)malloc(sizeof(musicNode));
p->num = i++;
strcpy(p->name, fileInfo.name);
q->next = p;
q = p;
printf("%s\n", fileInfo.name);
while (!_findnext(handle,&fileInfo))
{
printf("%s\n", fileInfo.name);
p = (linkMusic)malloc(sizeof(musicNode));
p->num = i++;
strcpy(p->name, fileInfo.name);
q->next = p;
q = p;
}
p->next = NULL;
}
_findclose(handle);
p = head->next;
FILE* fp=NULL;
char file[100] = "C:\\music\\测试.txt";
fp = fopen(file, "w+");
while (p)
{
printf("%d %s\n", p->num, p->name);
fprintf(fp,"%s\n",p->name);
p = p->next;
}
fclose(fp);
char name[100];
fp = fopen(file, "r+");
/*while (fgets(name, sizeof(name), fp) != NULL)
{
name[strlen(name) - 1] = '\0';
printf("test:%s", name);
}*/
/*while (!feof(fp))//根本没有用,我靠,最后的-1被读取进去了,所以要出错
{
memset(name, 0, sizeof(name));
fgets(name, sizeof(name), fp);
name[strlen(name) - 1] = '\0';
}*/
while (!feof(fp))//根本没有用,我靠,最后的-1被读取进去了,所以要出错
{
memset(name, 0, sizeof(name));
char* n= fgets(name, sizeof(name), fp);
if (n != NULL) {//这样就可以避免最后一个-1被读进去,所有读取的字符串都是具有换行符的
name[strlen(name) - 1] = '\0';
printf("test:%s", name);
}
}
fclose(fp);
//mciSendString("open C:\\music\\1.mp3 Alias music", 0, 0, 0);
/*char name[100] = "Adele - Rolling in the Deep.mp3";
char file1[100];
printf("%s\n", name);
GetShortPathName(name, name, 100);
sprintf(file1,"open %s Alias music",name);
printf("%s\n", file1);
printf("%d",mciSendString(file1, 0, 0, 0));
printf("%d", mciSendString("play music ", 0, 0, 0));*/
}
if (num == 2)
{
printf("输入2 暂停音乐;\n");
//c:music
mciSendString("pause music", 0, 0, 0);
}
if (num == 3)
{
//c:music
printf("输入3 继续音乐;\n");
mciSendString("resume music", 0, 0, 0);
}
if (num == 4)
{
//c:music
printf("输入4 停止音乐;\n");
mciSendString("stop music", 0, 0, 0);
}
}
getchar();
return 0;
}
以下是一个相对完整的版本
function.h
#ifndef _MYFUNC_
#define _MYFUNC_
struct music* HEAD, * pN;//定义一个全局链表头结点和一个当前工作节点
struct music* createSongList();//初始化链表
struct music* findSongList();//初始化查找列表
struct music* getPN(int num);//返回当前工作指针
void printLink(struct music* link);//打印链表
void printListLink(struct musicList * link);//打印音乐列表链表
void free_list(struct music* head);//释放链表
int countLink(struct music* head);//链表长度
//音乐相关
void playMusic(struct music* head, struct music* pT);//播放音乐
void pauseMusic(struct music* head, struct music* pT);//暂停音乐
void resumeMusic(struct music* head, struct music* pT);//继续音乐
void stopMusic(struct music* head, struct music* pT);//停止音乐
void beforeMusic(struct music* head, struct music* pT);//上一首
void nextMusic(struct music* head, struct music* pT);//下一首
void stepF(struct music* head, struct music* pT);//快进
void stepB(struct music* head, struct music* pT);//快退
void volUP(struct music* head, struct music* pT);//声音增大
void volD(struct music* head, struct music* pT);//声音减少
long getMusicLength(struct music*pT);//获取音乐长度
long getMusicPotion(struct music* pT);//获取音乐当前进度
void showTime(struct music* head, struct music* pT);//获取歌曲时间
boolean face();//是否调用,提示信息
#endif // !
function.c
#include
#include
#include
#include
#include
#include "function.h"
extern int num;
extern boolean op =FALSE;
char FILENAME[MAX_PATH] = "C:\\music\\";
int songLength;
int songCur;
//音乐链表
typedef struct music
{
int num;
char name[100];
struct music* next;
}musicNode,*linkMusic;
//音乐列表链表
typedef struct musicList {
int num;
char name[100];
struct musicList* next;
} *musicListLink, musicListNode;
struct music* createSongList() {
struct music* head, * p, * q;
//head = (struct music*)malloc(sizeof(struct music));
head = (linkMusic)malloc(sizeof(musicNode));
struct _finddata_t fileInfo;
long Handle;
char sFile[MAX_PATH];
wsprintf(sFile, "%s%s", FILENAME, "*.mp3");//拼接文件路径
if ((Handle = _findfirst(sFile, &fileInfo)) == -1L){
printf("查找文件失败");
head = NULL;
return head;
}
else {
int i = 1;
//p = (struct music*)malloc(sizeof(struct music));
p = (linkMusic)malloc(sizeof(musicNode));
q = head;
p->num = i;
strcpy(p->name, fileInfo.name);
q->next = p;
q = p;
while (_findnext(Handle,&fileInfo)==0)
{
i++;
p = (linkMusic)malloc(sizeof(musicNode));
p->num = i;
strcpy(p->name, fileInfo.name);
q->next = p;
q = p;
}
p->next = NULL;
}
_findclose(Handle);
return head;
}
struct music* findSongList()//初始化查找列表
{
//默认的txt文件路径为c:\music
char* songLists[20];
struct _finddata_t fileInfo;
long handle;
char sFile[100];
int lNum = 1;
FILE* fp = NULL;//定义一个文件指针
wsprintf(sFile, "%s%s", FILENAME, "*.txt");//拼接文件路径
musicListLink Lhead = (musicListLink)malloc(sizeof(musicListNode));//创建音乐列表链表
musicListLink Lp, Lq=Lhead;
handle = _findfirst(sFile, &fileInfo);
printf("输入3创建新的歌单,否则读取已有歌单\n");
int option = getch() - 48;
if (option ==3) {
while (1) {
HEAD = createSongList();
pN = HEAD->next;
printLink(HEAD);
char name[100];
char file[100];
printf("请输入歌单名称:\n");
scanf("%s", name);
sprintf(file, "%s%s.txt", FILENAME, name);
printf("%s\n", file);
linkMusic head = (linkMusic)malloc(sizeof(musicNode));//创建一个临时链表存储歌曲名
linkMusic p = head;
linkMusic q = p;
printf("请选择您要加入的歌曲序号:\n输入如果结束输入请输入q\n");
int num1;
scanf("%d", &num1);
while (num1 != 0)
{
printf("请选择您要加入的歌曲序号:\n输入如果结束输入请输入0\n");
scanf("%d", &num1);
if (num1 == 0) break;
pN = getPN(num1);
p = (linkMusic)malloc(sizeof(musicNode));
strcpy(p->name, pN->name);
q->next = p;
q = p;
}
p->next = NULL;
int num = countLink(head);
p = head->next;
fp = fopen(file, "w+");
for (int i = 0; i < num; i++)
{
//fputs(p->name, fp);
fprintf(fp, "%s\n", p->name);
p = p->next;
}
fclose(fp);
free_list(head);
printf("是否继续创建歌单:输入1继续;输入2完成歌单创建\n");
int numF = getch() - 48;
if (numF == 2)
{
break;
}
}
return NULL;
}
if (handle==-1)
{
printf("尚未创建歌单;\n如果要创建新的歌单请输入1;\n从媒体库播放请输入2\n");
int num;
num = getch()-48;
if (num== 1)
{
HEAD = createSongList();
pN = HEAD->next;
printLink(HEAD);
while (1) {
char name[100];
char file[100];
printf("请输入歌单名称:\n");
scanf("%s", name);
sprintf(file, "%s%s.txt", FILENAME, name);
printf("%s\n", file);
linkMusic head = (linkMusic)malloc(sizeof(musicNode));//创建一个临时链表存储歌曲名
linkMusic p = head;
linkMusic q = p;
printf("请选择您要加入的歌曲序号:\n输入如果结束输入请输入q\n");
int num1;
scanf("%d", &num1);
while (num1 != 0)
{
printf("请选择您要加入的歌曲序号:\n输入如果结束输入请输入0\n");
scanf("%d", &num1);
if (num1 == 0) break;
pN = getPN(num1);
p = (linkMusic)malloc(sizeof(musicNode));
strcpy(p->name, pN->name);
q->next = p;
q = p;
}
p->next = NULL;
int num = countLink(head);
p = head->next;
fp = fopen(file, "w+");
for (int i = 0; i < num; i++)
{
//fputs(p->name, fp);
fprintf(fp, "%s\n", p->name);
p = p->next;
}
fclose(fp);
free_list(head);
printf("是否继续创建歌单:输入1继续;输入2完成");
int numF = getch() - 48;
if (numF==2)
{
break;
}
}
return NULL;
}
else {
HEAD = createSongList();
printLink(HEAD);
printf("请选择您要播放的歌曲序号:\n");
//printf("%d\n",countLink(HEAD));
int num1;
scanf("%d", &num1);
pN = getPN(num1);
playMusic(HEAD, pN);
}
}else{
Lp = (musicListLink)malloc(sizeof(musicListNode));
Lp->num = lNum;
strcpy(Lp->name, fileInfo.name);
Lq->next = Lp;
Lq = Lp;
while (!_findnext(handle,&fileInfo))
{
Lp = (musicListLink)malloc(sizeof(musicListNode));
Lp->num = ++lNum;
strcpy(Lp->name, fileInfo.name);
Lq->next = Lp;
Lq = Lp;
}
Lp->next = NULL;
Lp = Lhead;
_findclose(handle);
printListLink(Lp);
Lp = Lhead;
printf("请输入歌单编号选择歌单:\n");
linkMusic head1, p1, q1;
head1 = (linkMusic)malloc(sizeof(musicNode));
q1 = head1;
int num;
scanf("%d", &num);
while (Lp)
{
if (Lp->num == num )
{
sprintf(sFile, "%s%s", FILENAME, Lp->name);
printf("%s\n", sFile);
fp=fopen(sFile, "r+");
int i = 1;
char name[100];
while (fgets(name,sizeof(name),fp)!=NULL)//记住用fgets获取数据一定要通过这个来
{
p1 = (linkMusic)malloc(sizeof(musicNode));
//if (!fgets(p1->name, sizeof(p1->name), fp))
//{
name[strlen(name) - 1] = '\0';
strcpy(p1->name, name);
p1->num = i++;
q1->next = p1;
q1 = p1;
printf("%d %s\n",p1->num, p1->name);
//}
}
p1->next = NULL;
fclose(fp);
HEAD = head1;
printLink(head1);
printf("请选择您要播放的歌曲序号:\n");
//printf("%d\n",countLink(HEAD));
int num1;
scanf("%d", &num1);
pN = getPN(num1);
playMusic(HEAD, pN);
break;
}
Lp = Lp->next;
}
}
}
struct music* getPN(int num)//返回当前工作指针
{
linkMusic p=HEAD->next;
if (pN != NULL) {
stopMusic(HEAD, pN);
}
while (p)
{
if (p->num == num) {
return p;
}
p = p->next;
}
return NULL;
}
//打印链表
void printLink(struct music* link) {
if (!link->next)
{
printf("无歌曲!");
return;
}
link = link->next;
int i = 1;
while (link)
{
printf("%d %s\n",i++,link->name);
link = link->next;
}
}
void printListLink(struct musicList* link)//打印音乐列表链表
{
if (!link->next)
{
printf("无歌单!");
return;
}
link = link->next;
int i = 1;
while (link)
{
printf("%d %s\n", i++, link->name);
link = link->next;
}
}
int countLink(struct music* head)//链表长度
{
linkMusic p = head;
int i = 0;
while (p)
{
i++;
p = p->next;
}
return i-1;
}
void free_list(struct music* head)//释放链表
{
linkMusic p;
while (head)
{
p = head;
head = head->next;
free(p);
p = NULL;
}
}
//音乐相关
void playMusic(struct music* head, struct music* pT)//播放音乐
{
char fName[MAX_PATH];
char cmd[MAX_PATH];
wsprintf(fName, "%s%s", FILENAME, pT->name);
GetShortPathName(fName, fName, 50);//获取文件短路径
wsprintf(cmd, "open %s", fName);//当你取了别名以后就要用别名,再去拼接地址就没有用了
printf("%s", cmd);
printf("%d", mciSendString(cmd, NULL, 0, NULL));
//volD(head,pT);
wsprintf(cmd, "seek %s to start",fName);//每次播放要定位到歌曲的开头去,要不然上一曲下一曲会有记录
mciSendString(cmd, NULL, 0, 0);
wsprintf(cmd, "play %s", fName);
printf("%d", mciSendString(cmd, NULL, 0, NULL));
//mciSendString("play music", NULL, 0, NULL);//与后面的要统一
showTime(head,pT);
}
void pauseMusic(struct music* head, struct music* pT)//暂停音乐
{
char cmdp[100];
char file[100];
wsprintf(cmdp, "%s%s", FILENAME,pT->name);
//printf("%s\n", cmdp);
GetShortPathName(cmdp, cmdp, 100);
wsprintf(file, "pause %s", cmdp);
//printf("%s\n", file);
printf("%d",mciSendString(file, NULL, 0, NULL));
//mciSendString("pause music", NULL, 0, NULL);
}
void resumeMusic(struct music* head, struct music* pT)//继续音乐
{
char cmdR[100];
char file[100];
wsprintf(cmdR, "%s%s", FILENAME, pT->name);
//printf("%s\n", cmdp);
GetShortPathName(cmdR, cmdR, 100);
wsprintf(file, "resume %s", cmdR);
mciSendString(file, NULL, 0, NULL);
showTime(head, pT);
//mciSendString("resume music", NULL, 0, NULL);
}
void stopMusic(struct music* head,struct music* pT)//停止音乐
{
char cmdS[100];
char file[100];
wsprintf(cmdS, "%s%s", FILENAME, pT->name);
//printf("%s\n", cmdp);
GetShortPathName(cmdS, cmdS, 100);
wsprintf(file, "stop %s", cmdS);
mciSendString(file, NULL, 0, NULL);
printf("停止:%s\n", pT->name);
//mciSendString("stop music", NULL, 0, NULL);
}
void beforeMusic(struct music* head, struct music* pT)//上一首
{
if (pT->num == 1) {
printf("这是第一首歌不能上一曲;");
Sleep(5000);
return;
};
stopMusic(HEAD, pT);
linkMusic p= head;
while (p)
{
if (p->next->num==pT->num)
{
pN = p;
printf("num===:%d", pN->num);
playMusic(HEAD, pN);
return;
}
p = p->next;
}
}
void nextMusic(struct music* head, struct music* pT)//下一首
{
int num = countLink(head);
printf("%d\n", num);
if (pT->num == num)
{
printf("当前已是最后一首歌请选择上一曲;");
Sleep(5000);
return;
}
stopMusic(head, pT);
linkMusic p = head->next;
while (p)
{
if (strcmp(p->name, pT->name)==0) {//如果找到了该歌曲就去找之后的节点
pN = pT->next;
playMusic(head, pN);
return;
}
p = p->next;
}
}
void stepF(struct music* head, struct music* pT)//快进
{
long len = getMusicLength(pT);
long lenCur = getMusicPotion(pT);
long p1, p2;
p1 =lenCur +10000;//毫秒为单位的,每点击一次前进10s
//p1 = 50000;
MCIERROR mciError;
char buf[50];
char cmd[MAX_PATH];
char file[MAX_PATH];
wsprintf(file, "%s%s", FILENAME, pT->name);
GetShortPathName(file, file, sizeof(file));
//wsprintf(cmd, "seek %s to start",file);到开头去
//mciSendString(cmd, null, 0, 0);
wsprintf(cmd, "seek %s to %i", file,p1);
mciError=mciSendString(cmd, 0, 0, 0);
wsprintf(cmd, "play %s", file);
mciSendString(cmd, 0, 0, 0);
if (mciError) {
mciGetErrorString(mciError, buf, sizeof(buf));
puts(buf);
Sleep(3000);
}
showTime(head, pT);
}
void stepB(struct music* head, struct music* pT)//快退
{
long len = getMusicLength(pT);
long lenCur = getMusicPotion(pT);
long p1, p2;
p1 = lenCur - 10000;//毫秒为单位的,每点击一次退后10s
//p1 = 50000;
MCIERROR mciError;
char buf[50];
char cmd[MAX_PATH];
char file[MAX_PATH];
wsprintf(file, "%s%s", FILENAME, pT->name);
GetShortPathName(file, file, sizeof(file));
//wsprintf(cmd, "seek %s to start",file);到开头去
//mciSendString(cmd, p1, 0, 0);
wsprintf(cmd, "seek %s to %i", file, p1);
mciError = mciSendString(cmd, 0, 0, 0);
wsprintf(cmd, "play %s", file);
mciSendString(cmd, 0, 0, 0);
if (mciError) {
mciGetErrorString(mciError, buf, sizeof(buf));
puts(buf);
Sleep(3000);
}
showTime(head, pT);
}
void volUP(struct music* head, struct music* pT)//声音增大
{
if (pT == NULL)
{
printf("当前无歌曲播放,请选择歌曲播放");
return;
}
char cmd[MAX_PATH];//存储媒体的操作命令
char file[MAX_PATH];//存储路径
char volume[100];//存储当前音量
long vol;//整型音量
//构建短路径,去除空格的影响
sprintf(file, "%s%s", FILENAME, pT->name);
GetShortPathName(file, file, sizeof(file));
//构建操作命令,并发送命令
sprintf(cmd, "status %s volume", file);//生成媒体命令
mciSendString(cmd, volume, sizeof(volume), 0);//获取当前音量至volume字符串中
vol = atoi(volume);//将获取到的音量字符串装换成整数
printf("当前音量%i", vol);
//设置音量
wsprintf(cmd, "setaudio %s volume to %i", file, vol+100);//生成媒体操作命令,音量加50
printf("打印地址%s\n", cmd);
printf("返回值:%d",mciSendString(cmd, "", 0, NULL));//执行媒体命令
}
void volD(struct music* head, struct music* pT)//声音减少
{
if (pT==NULL)
{
printf("当前无歌曲播放,请选择歌曲播放");
return;
}
char cmd[MAX_PATH];//存储媒体的操作命令
char file[MAX_PATH];//存储路径
char volume[100];//存储当前音量
long vol;//整型音量
MCIERROR mciError;
char buf[50];
//构建短路径,去除空格的影响
sprintf(file, "%s%s", FILENAME, pT->name);
GetShortPathName(file, file, sizeof(file));
//构建操作命令,并发送命令
sprintf(cmd, "status %s volume", file);//生成媒体命令
mciSendString(cmd, volume, sizeof(volume), 0);//获取当前音量至volume字符串中
vol = atoi(volume);//将获取到的音量字符串装换成整数
printf("当前音量%i\n", vol);
//设置音量
sprintf(cmd, "setaudio %s volume to %i", file, vol - 100);//生成媒体操作命令,音量减50
mciError = mciSendString(cmd, NULL, 0, NULL);//执行媒体命令
printf("打印地址%s\n", cmd);
if (mciError) {
mciGetErrorString(mciError, buf, sizeof(buf));
puts(buf);
Sleep(3000);
}
//构建操作命令,并发送命令
sprintf(cmd, "status %s volume", file);//生成媒体命令
mciSendString(cmd, volume, sizeof(volume), 0);//获取当前音量至volume字符串中
vol = atoi(volume);//将获取到的音量字符串装换成整数
printf("当前音量1:%i", vol);
}
long getMusicLength(struct music* pT)//获取音乐长度
{
char length[100];
char cmd[MAX_PATH];
char file[MAX_PATH];
wsprintf(file, "%s%s", FILENAME, pT->name);
GetShortPathName(file, file, sizeof(file));
wsprintf(cmd, "status %s length", file);
mciSendString(cmd, length, sizeof(length), 0);
int le = atoi(length);
return le;
}
long getMusicPotion(struct music* pT)//获取音乐当前进度
{
char cmd[MAX_PATH];
char file[MAX_PATH];
wsprintf(file, "%s%s", FILENAME, pT->name);
GetShortPathName(file, file, sizeof(file));
char sPosition[255];
wsprintf(cmd, "status %s position", file);
mciSendString(cmd, sPosition, 255, 0);
long length_st = strtol(sPosition, NULL, 10);
return length_st;
}
void showTime(struct music* head, struct music* pT)//获取歌曲时间
{
char time[100];
char time1[100];
while (!_kbhit()) {
system("cls");
face();
long len = getMusicLength(pT);
long lenCur = getMusicPotion(pT);
wsprintf(time, "%i:%i", (int)(len / 1000 / 60), (int)(len / 1000 % 60));
wsprintf(time1, "%i:%i", (int)(lenCur / 1000 / 60), (int)(lenCur / 1000 % 60));
printf("当前播放的歌曲为:%s,当前进度为:%s/%s",pT->name, time1, time);
Sleep(1000);
if (len == lenCur) {
pN = pT->next;
nextMusic(head, pN);
return;
}
};//如果有鼠标的活动就会返回一个非零值,否则返回0
if (_kbhit())
{
system("cls");
op = face();
printf("当前播放的歌曲为:%s,当前进度为:%s/%s", pT->name, time1, time);
}
}
boolean face() {//是否调用
printf("输入1 播放音乐\n");
printf("输入2 暂停音乐\n");
printf("输入3 继续音乐\n");
printf("输入4 停止音乐\n");
printf("输入5 上一曲\n");
printf("输入6 下一曲\n");
printf("输入7 增加音量\n");
printf("输入8 减少音量\n");
printf("输入i 快进\n");
printf("输入d 快退\n");
printf("输入9 退出\n");
return TRUE;
}
main.c
#include
#include
#include
#include
#include
#include"function.h"
char file[MAX_PATH] = "C:\\music\\";
int num;
boolean op;
int main() {
//mp3
//1:实现音乐的播放
//2.从文件中读取音乐的信息
//3.如何存储读取的信息
printf("**************************************************\n");
printf("***********************mp3简化版******************\n");
printf("**************************************************\n");
while (1)
{
if (!op)
{
op = face();
}
//printf("op=%d", op);
num = _getch() - 48;//将得到的字符数字变成int型,这个函数不会将输入的数字显示在屏幕上,没有缓存
if (num > 9)
{
num += 48;
}
if (num==1)
{
printf("输入1 播放音乐\n");
printf("///////////////////创建链表//////////////////////////////////////\n");
printf("输入1打开媒体库\n输入2打开已有歌单\n");
int option;
//scanf("%d", &option);
option = _getch()-48;
if (option==1)
{
HEAD = createSongList();
printLink(HEAD);
printf("请选择您要播放的歌曲序号:\n");
//printf("%d\n",countLink(HEAD));
int num1;
scanf("%d", &num1);
pN = getPN(num1);
playMusic(HEAD, pN);
}
if (option == 2) {
//从文件中读取已有歌单
if (!findSongList()){findSongList();};
}
else
{
printf("请正确输入数字");
}
}
if (num == 2)
{
printf("输入2 暂停音乐\n");
/*char cmdp[100];
wsprintf(cmdp, "pause %s", file);
mciSendString(cmdp, NULL, 0, NULL);*/
//char name[] = "永不失联的爱 .mp3";
pauseMusic(HEAD, pN);
/*char cmdp[100];
char file[100];
wsprintf(cmdp, "%s%s", FILENAME, name);
printf("%s\n", cmdp);
GetShortPathName(cmdp, cmdp, 100);
wsprintf(file, "pause %s", cmdp);
printf("%s\n", file);
printf("%d", mciSendString(file, NULL, 0, NULL));*/
}
if (num == 3)
{
/*printf("输入3 继续音乐\n");
mciSendString("resume file", NULL, 0, NULL);*/
//char cmdR[100];
//wsprintf(cmdR, "resume %s", file);
//mciSendString(cmdR, NULL, 0, NULL);
resumeMusic(HEAD, pN);
}
if (num == 4)
{
printf("输入4 停止音乐\n");
//mciSendString("stop file", NULL, 0, NULL);
//char cmdS[100];
//wsprintf(cmdS, "stop %s", file);
//mciSendString(cmdS, NULL, 0, NULL);
stopMusic(HEAD, pN);
}
if (num == 5) {
printf("上一曲");
beforeMusic(HEAD, pN);//上一首
}
if (num == 6) {
printf("下一曲");
nextMusic(HEAD, pN);
}
if (num == 7) {
printf("音量增");
volUP(HEAD, pN);
}
if (num == 8) {
printf("音量减");
volD(HEAD, pN);
}
if (num == 'i') {
printf("快进");
stepF(HEAD, pN);
}
if (num == 'd') {
printf("快退");
stepB(HEAD, pN);
}
if (num == 9)
{
printf("输入9 退出\n");
exit(0);
}d
}
printf("hello");
return 0;
}