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************************
");
printf("1. play/pause
");
printf("2. next
");
printf("3. prev
");
printf("4. stop
");
printf("5. exit
");
printf("**************************************
");
list_show();
int choose =4;
do{
printf(" > ");
scanf("%d",&choose);
if(choose>=0&&choose<=4)
break;
printf("choose invalid
");
while(getchar()!='
');
}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("
");
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
",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;
}
인 스 턴 스 효과 그림 은 다음 과 같 습 니 다.이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 구현 천둥 제거 게임 상세 정보먼저 작은 메뉴를 표시하고 게임을 할지 여부를 선택하십시오.사용자가 종료를 선택하면 프로그램 실행이 끝나고, 사용자가 게임을 선택하면 지뢰 제거 위치 좌표를 입력하라는 메시지가 표시됩니다.사용자가 입력한 좌표가 바둑...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.