读取lrc歌词文件读取lrc歌词文件
/*将名为 擦肩而过.mp3 和 擦肩而过.lrc 的歌词文件放到工程目录便可运
行*/
#include
#include
#include
#define TEXT_RED SetConsoleTextAttribute(consolehwnd,FOREGROUND_RED); #define TEXT_GREEN SetConsoleTextAttribute(consolehwnd,FOREGROUND_GREEN); #define TEXT_INTENSITY
SetConso...
读取lrc歌词文件
/*将名为 擦肩而过.mp3 和 擦肩而过.lrc 的歌词文件放到
目录便可运
行*/
#include
#include
#include
#define TEXT_RED SetConsoleTextAttribute(consolehwnd,FOREGROUND_RED); #define TEXT_GREEN SetConsoleTextAttribute(consolehwnd,FOREGROUND_GREEN); #define TEXT_INTENSITY
SetConsoleTextAttribute(consolehwnd,FOREGROUND_INTENSITY); //亮
#define TEXT_BLUE SetConsoleTextAttribute(consolehwnd,FOREGROUND_BLUE);
//蓝
#define TEXT_PURPLE
SetConsoleTextAttribute(consolehwnd,FOREGROUND_RED|FOREGROUND_BLUE); //紫
#define TEXT_PALM
SetConsoleTextAttribute(consolehwnd,FOREGROUND_RED|FOREGROUND_GREEN); //棕
#define TEXT_BRIGHT_RED
SetConsoleTextAttribute(consolehwnd,FOREGROUND_RED|FOREGROUND_INTENSITY); //亮红
#define TEXT_SKY_BLUE
SetConsoleTextAttribute(consolehwnd,FOREGROUND_BLUE|FOREGROUND_GREEN); //天蓝
#define TEXT_BRIGHT_BLUE
SetConsoleTextAttribute(consolehwnd,FOREGROUND_BLUE|FOREGROUND_INTENSITY); //亮蓝
#define TEXT_BRIGHT_GREEN
SetConsoleTextAttribute(consolehwnd,FOREGROUND_GREEN|FOREGROUND_INTENSITY); //亮红
HANDLE consolehwnd; //用于修改颜色
char songname[40] = "擦肩而过.MP3";
char lrcname[40] = "擦肩而过.lrc";
typedef struct song_lrc
{
char lrc[100];
int time;
struct song_lrc *next;
struct song_lrc *pre;
}lrc_t;
int music_lrc(lrc_t *head); void music_lrc_sort(lrc_t *head); void music_lrc_double(lrc_t *head); int music_position(lrc_t * head); void Cursor_Hide (void);
void list_del(lrc_t *head); //删除链
//两个函数
void gotoxy(int x, int y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main (void)
{
lrc_t head = {0};
char playing[50] = {0};
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); //文本颜色
Cursor_Hide();
mciSendString("open", NULL, 0, NULL); //初始化mci模块
sprintf(playing, "play %s", songname);
mciSendString(playing, NULL, 0, NULL); //播放一首歌
music_lrc(&head); //创建歌词链表
music_lrc_sort(&head); //将歌词链表排序
music_lrc_double(&head); //将双链表接好
while(1)
{
if (music_position(&head)) //同步显示
{
break; //放完退出
}
}
list_del(&head);
mciSendString("close", NULL, 0, NULL); //关闭mci模块
return 0;
}
void list_del(lrc_t *head) //删除链表
{
lrc_t *del = NULL;
while(NULL != head->next)
{
del = head->next;
head->next = del->next;
free(del);
}
return ;
}
void music_lrc_double(lrc_t *head) //将双链表链接起来 {
lrc_t *temp = head;
lrc_t *tempnext = head->next;
while(temp->next != NULL)
{
tempnext->pre = temp;
temp = temp->next;
tempnext = tempnext->next;
}
}
int music_position(lrc_t * head) //把歌词和播放时间同步 {
static int i = 0;
char buff[50] = {0};
char playing[50] = {0};
long playingtime = 0;
long alltime = 0;
static lrc_t *temp = NULL;
static lrc_t *position = NULL;
if (i == 0)
{
temp = head->next;
}
sprintf(playing, "Status %s length", songname);
mciSendString (playing, buff, sizeof(buff), NULL);//以字符串的方式得到歌曲总时间
alltime = strtol(buff, NULL, 10);
sprintf(playing, "Status %s position", songname);
mciSendString (playing, buff, sizeof(buff), NULL); //获取歌曲现在正在播放时间
playingtime = strtol(buff, NULL, 10);
if (playingtime > (temp->time - 500)) //和现在正在播放的时间点匹配上了,歌词滚动一下
{
position = temp;
gotoxy(0, 0);
for (i = 0; i < 3; i++) //匹配上的时间点,向上退三行
{
if (position->pre != NULL)
{
position = position->pre;
}
}
for (i = 0; i < 7; i++) //显示七行
{
if (position->next != NULL)
{
if (i == 3) //第三行为红色
{
TEXT_RED
}
else
{
TEXT_GREEN //其它为绿色
}
printf("%s \n", position->lrc);
position = position->next;
}
else
{
printf("
");
}
}
if (temp->next != NULL)
{
temp = temp->next;
}
}
if (alltime - playingtime < 2000) //歌曲放完
{
return 1;
}
return 0;
}
int get_time(char *buf) { //获取歌词时间标签,返回得到的时间
int time = 0;
static int pretime = 0;
if ((buf[0] == '[') && (buf[1] == '0')) //如果是正常的时间,取出时间
{
time = ((((buf[1] - '0') * 10 + (buf[2] - '0')) * 60) + ((buf[4] - '0') *
10 +
(buf[5] - '0'))) * 1000 + (buf[7] - '0') * 10 + buf[8] - '0';
pretime = time;
return time;
}
else //如果不是正常时间,返回上次时间+1
{
return pretime+1;
}
}
char *get_lrc(char *buff) //获取正常歌词 {
int i = 0;
while((buff[i] == '[') || (buff[i] == ']') || (buff[i] == ':') ||
(buff[i] == '.') || (buff[i] >= '0' && buff[i] <= '9'))
i++;
return buff + i;
}
int music_lrc(lrc_t *head)
{ //歌词,创建歌词链表
int nextpostion = 0; //找下了个时间标记
int findend = 0; //找标题结束标记
lrc_t *temp = head;
char titlebuff[40]; //暂存标题
lrc_t *node = NULL;
int cou = 0; //统计有几个 [ ]
int i = 0;
FILE *fp = fopen(lrcname, "r");
char *buff = (char *)malloc(100);
do
{
i = 0;
cou = 0;
fscanf(fp, "%s", buff);
while(buff[i] != '\0')
if (buff[i++] == '[')cou++;
nextpostion = 0;
for (i = 0; i < cou; i++)
{
node = (lrc_t *)malloc(sizeof(lrc_t));
if (buff[1] == '0') //如果是正常的歌词
{
node->time = get_time(buff+nextpostion); //获取时间
strcpy(node->lrc, get_lrc(buff)); //获取歌词
temp->next = node;
head->time++; //歌词链表结点计数
temp = temp->next;
nextpostion += 10;
}
else if (buff[1] == 't' || buff[1] == 'a') //如果是标题或者作者或者其它
{
findend = 0; //初始化歌词结束标签
if (buff[2] == 'i') //如果是标题标签
{
node->time = 0;
memset(titlebuff, 0, sizeof(titlebuff));
sprintf(titlebuff, "歌曲名是:%s", buff+4);
while(titlebuff[findend++] != ']');
titlebuff[findend-1] = '\0';
strcpy(node->lrc, titlebuff);
temp->next = node;
head->time++;
temp = temp->next;
}
else if (buff[2] == 'r') //如果是作者标签
{
node->time = 0;
memset(titlebuff, 0, sizeof(titlebuff));
sprintf(titlebuff, "演唱是:%s", buff+4);
while(titlebuff[findend++] != ']');
titlebuff[findend-1] = '\0';
strcpy(node->lrc, titlebuff);
temp->next = node;
head->time++;
temp = temp->next;
}
else if (buff[2] == 'l') //如果是歌曲名标签
{
node->time = 0;
memset(titlebuff, 0, sizeof(titlebuff));
sprintf(titlebuff, "作词是:%s", buff+4);
while(titlebuff[findend++] != ']');
titlebuff[findend-1] = '\0';
strcpy(node->lrc, titlebuff);
temp->next = node;
head->time++;
temp = temp->next;
}
}
else //其它,前面没有标记的,作为普通歌词
{
node->time = get_time(buff+nextpostion);
strcpy(node->lrc, buff);
temp->next = node;
head->time++;
temp = temp->next;
}
}
} while (!feof(fp)); //歌词文件读完了
node->next = NULL;
fclose(fp);
return 0;
}
void music_lrc_sort(lrc_t *head) {//歌词链表按显示时间排序
int i = 0;
lrc_t *al = head;
lrc_t *pre = NULL;
lrc_t *sw = NULL;
lrc_t *temp = NULL;
lrc_t *tempnext = NULL;
for(i = 0; i < head->time - 1; i++) //冒泡排序法对链表排序
{
al = head;
while(al->next->next != NULL)
{
pre = al;
temp = al->next;
tempnext = al->next->next;
if (temp->time > tempnext->time)
{
sw = tempnext;
temp->next = sw->next;
sw->next = pre->next;
pre->next = sw;
}
al = al->next;
}
}
return ;
}
void Cursor_Hide (void) {//隐藏光标函数
CONSOLE_CURSOR_INFO CCI;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CCI);
CCI.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CCI);
}
本文档为【读取lrc歌词文件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。