服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言音乐播放器实例代码

C语言音乐播放器实例代码

2021-06-27 19:13lemon100521 C/C++

文章给大家分享了用C语言音乐播放器的实例代码,对此有需要的朋友参考学习下。

实例代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#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;
}

实例效果图片如下:

C语言音乐播放器实例代码

 

原文链接:https://blog.csdn.net/qq_40409115/article/details/80931790

延伸 · 阅读

精彩推荐