mp3-player/PlayInterface.c

62 lines
1.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <Windows.h>
#include <mmsystem.h> // mci库头文件
#pragma comment(lib, "winmm.lib") // 指定MCI库mciSendString函数的定义在winmm.lib中
// 播放当前曲曲号由curno记录
void play(const char *name) //播放音乐
{
char cmd[MAX_PATH] = {0};
char pathname[MAX_PATH] = {0};
// 加路径
sprintf(pathname, ".\\music\\%s", name);
// GetShortPathName用来转换短名要求被转换的歌名必须能在指定目录下找到文件否则转换失败。
// 第一个参数:源文件名,第二个参数:目的文件名,第三个参数:目的数组长度。
GetShortPathName(pathname, pathname, MAX_PATH);
// 定义发往MCI的命令cmd指定命令存储的数组后面参数跟printf()相同
sprintf(cmd, "open %s", pathname);
// 发送命令。
// 一、存储命令的数组首地址二、接受MCI返回的信息三、接受数组的长度四、没用NULL
mciSendString(cmd, "", 0, NULL);
sprintf(cmd, "play %s", pathname);
mciSendString(cmd, "", 0, NULL);
}
// 暂停当前曲曲号由curno记录
void pause(const char *name) // 暂停播放
{
char cmd[MAX_PATH] = {0};
char pathname[MAX_PATH] = {0};
// 加路径
sprintf(pathname, ".\\music\\%s", name);
// GetShortPathName用来转换短名要求被转换的歌名必须能在指定目录下找到文件否则转换失败。
// 第一个参数:源文件名,第二个参数:目的文件名,第三个参数:目的数组长度。
GetShortPathName(pathname, pathname, MAX_PATH);
sprintf(cmd, "pause %s", pathname);
mciSendString(cmd,"",0,NULL);
}
// 停止当前曲曲号由curno记录
void stop(const char *name)
{
char cmd[MAX_PATH] = {0};
char pathname[MAX_PATH] = {0};
// 加路径
sprintf(pathname, ".\\music\\%s", name);
// GetShortPathName用来转换短名要求被转换的歌名必须能在指定目录下找到文件否则转换失败。
// 第一个参数:源文件名,第二个参数:目的文件名,第三个参数:目的数组长度。
GetShortPathName(pathname, pathname, MAX_PATH);
sprintf(cmd, "stop %s", pathname);
mciSendString(cmd,"",0,NULL);
sprintf(cmd, "close %s", pathname);
mciSendString(cmd,"",0,NULL);
}