C语言实现的音乐播放器

C语言实现音乐播放器

#include <stdio.h>
#include<dirent.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>


typedef struct node_ node_t;
struct node_{
  char* name;//gequming
  node_t * prev;
  node_t * next;
};

node_t *head = NULL;
int first=1;//diyicibofnag
node_t * cur =NULL;//dangqianbofang

enum{STOP,PAUSE,PLAY};
int status = STOP;


void List_init(void){
 head = malloc(sizeof(node_t));
    memset(head,0x00,sizeof(node_t));
    head->next = head->prev=head;
}

void list_insert(const char* name){
  node_t *p = malloc(sizeof(node_t));
  memset(p,0x00,sizeof(node_t));

  p->name = malloc(strlen(name)+1);
  strcpy(p->name,name);

  p->next = head->next;
  p->prev = head;
  head->next->prev = p;
  head->next = p;
}


int menu(void){
  printf("*************menu************************\n");
  printf("1.  play/pause\n");
  printf("2.  next\n");
  printf("3.  prev\n");
  printf("4.  stop\n");
  printf("5.  exit\n");
  printf("**************************************\n");
  list_show();
  int choose =4;

  do{

     printf(" > ");
  scanf("%d",&choose);
  if(choose>=0&&choose<=4)
    break;
    printf("choose invalid\n");
    while(getchar()!='\n');
    }while(1);
    return choose;
}

void list_show(void){
  node_t *p = head->next;
  while(p!=head){
    printf("%s ",p->name);
     if(p==cur)
    printf("<<==cur");
    printf("\n");
    p = p->next;
 }
}


void load_music(const char * path){
   DIR * pdir = opendir(path);
  if(pdir == NULL){
    perror("opendir");
    exit(1);
}
struct dirent * p = NULL;
while((p=readdir(pdir))!=NULL){

if(p->d_name[0]=='.')
   continue;
   list_insert(p->d_name);
}

closedir(pdir);
}

void playPause(){
   if(first==1){
      char buf[1024] = {};
      sprintf(buf,"madplay -o wav:- ./music/Music/%s 2> /dev/null | aplay 2>/dev/null &",cur->name);
      system(buf);
      first = 0;
      status =  PLAY;
    }else{
     if(status==PLAY){
        system("killall -SIGSTOP aplay");
        status = PAUSE;
      }else if(status==PAUSE){
        system("killall -SIGCONT aplay");
        status = PLAY;
      }
  }
}
void stop(){
   system("killall -SIGKILL aplay");
   first=1;
}
void next(){
    stop();
    cur = cur ->next;
    if(cur==head){
      cur = cur->next;
 }
     playPause();
}
void prev(){
   stop();
   cur = cur->prev;
   if(cur==head){
    cur= cur->prev;
  }
   playPause();
}


int main(int args,char * argv[])
{
 List_init();
 load_music("./music/Music");
if(head->next!=head)
     cur = head->next;
 //printf("%s\n",cur->name);
 //list_show();
 do{
  int choose = menu();
  switch(choose){
    case 1:
          playPause();
          break;
    case 2:
         next();
         break;
    case 3:
         prev();
         break;
    case 4:
        stop();
        break;
    case 0:
        printf("thanks");
        system("killall -SIGKILL aplay");
        exit(0);
        break;
     default:
        break;
        //do nothing;
     }
}while(1);
 return 0;
}

这里写图片描述

  • 18
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
这是一个使用C语言编写的简易音乐播放器,主要实现了歌词与歌曲之间的同步。具体实现原理是将歌词切割并存入数组中,通过sleep函数创建虚拟时钟与歌词中的时间相对应。然后创建一个进程调用mplayer播放歌曲,同时开始计时就可以完成歌词与歌曲之间的同步。以下是主要代码: 1. my_lrc_play.h ```c #ifndef MY_LRC_PLAY_H #define MY_LRC_PLAY_H void my_lrc_play(); #endif ``` 2. my_lrc_play.c ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include "my_lrc_play.h" #define MAX_LRC_LINE 1024 #define MAX_LRC_SIZE 1024 typedef struct lrc_node { int time; char lrc[MAX_LRC_SIZE]; struct lrc_node *next; } LrcNode; LrcNode *head = NULL; void *lrc_thread(void *arg) { int time = 0; LrcNode *p = head; while (p != NULL) { if (time == p->time) { printf("%s\n", p->lrc); p = p->next; } sleep(1); time++; } return NULL; } void my_lrc_play() { FILE *fp = fopen("test.lrc", "r"); if (fp == NULL) { printf("Open file failed!\n"); return; } char buf[MAX_LRC_LINE]; while (fgets(buf, MAX_LRC_LINE, fp) != NULL) { int len = strlen(buf); if (len <= 10) { continue; } LrcNode *node = (LrcNode *) malloc(sizeof(LrcNode)); node->time = (buf[1] - '0') * 600 + (buf[2] - '0') * 60 + (buf[4] - '0') * 10 + (buf[5] - '0'); strcpy(node->lrc, buf + 10); node->next = NULL; if (head == NULL) { head = node; } else { LrcNode *p = head; while (p->next != NULL) { p = p->next; } p->next = node; } } pthread_t tid; pthread_create(&tid, NULL, lrc_thread, NULL); char *song_path = "test.mp3"; pid_t pid; pid = fork(); if (pid < 0) { perror("fork"); } else if (pid == 0) { close(1); close(2); execlp("mplayer", "mplayer", "-slave", "-quiet", song_path, NULL); exit(0); } else; wait(NULL); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值