資源簡介
基于ARM平臺的MP3播放器設計,開發平臺是linux、資料包含應用程序源碼,實現MP3播放控制。
代碼片段和文件信息
/*
?*?????mp3播放器控制程序
?* ???功能:
? ?k1:播放、暫停
? ?k2:停止播放
? ?k3:上一首
? ?k4:下一首
?*?????附加:歌曲自動循環播放
?*
?*/
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
/*共享內存申請標記*/
#define?PERM?S_IRUSR|S_IWUSR
/*雙向循環列表:存放歌曲名*/
struct?song
{
char?songname[20];
struct?song?*prev;
struct?song?*next;
};
/*孫子進程id號*/
pid_t?gradchild;
/*子進程id號*/
pid_t?pid;
/*共享內存描述標記*/
int?shmid;
char?*p_addr;
/*播放標記*/
int?first_key=1;
int?play_flag=0;
/*************************************************
Function?name:?play
Parameter????:?struct?song?*
Description ?:?播放函數
Return ?:?void
Argument?????:?void
Autor?&?date?:?ada?091207
**************************************************/
void?play(struct?song?*currentsong)
{
pid_t?fd;
char?*c_addr;
char?*p;
int?len;
char?my_song[30]=“/mp3/song/“;
while(currentsong)
{
/*創建子進程,即孫子進程*/
fd?=?fork();
if(fd?==?-1)
{
perror(“fork“);
exit(1);
}
else?if(fd?==?0)
{
/*把歌曲名加上根路徑*/
strcat(my_songcurrentsong->songname);
p?=?my_song;
len?=?strlen(p);
/*去掉文件名最后的‘\n‘*/
my_song[len-1]=‘\0‘;
printf(“THIS?SONG?IS?%s\n“my_song);
execl(“/mp3/madplay““madplay“my_songNULL);
printf(“\n\n\n“);
}
else
{
/*內存映射*/
c_addr?=?shmat(shmid00);
/*把孫子進程的id和當前播放歌曲的節點指針傳入共享內存*/
memcpy(c_addr&fdsizeof(pid_t));
memcpy(c_addr?+?sizeof(pid_t)+1¤tsong4);
/*使用wait阻塞孫子進程,直到孫子進程播放完才能被喚醒;
??當被喚醒時,表示播放MP3期間沒有按鍵按下,則繼續順序播放下一首MP3*/
if(fd?==?wait(NULL))
{
currentsong?=?currentsong->next;
printf(“THE?NEXT?SONG?IS?%s\n“currentsong->songname);
}
}
}
}
/*************************************************
Function?name:?creat_song_list
Parameter????:?void
Description ?:?創建歌曲名的雙向循環鏈表
Return ?:?struct?song?*
Argument?????:?void
Autor?&?date?:?ada?09.12.07
**************************************************/
struct?song?*creat_song_list(void)
{
FILE?*fd;
size_t?size;
size_t?len;
char?*line?=?NULL;
struct?song?*head;
struct?song?*p1;
struct?song?*p2;
system(“ls?/mp3/song?>song_list“);
fd?=?fopen(“song_list““r“);
p1?=?(struct?song?*)malloc(sizeof(struct?song));
printf(“==================================song?list=====================================\n“);
system(“ls?/mp3/song“);
printf(“\n“);
printf(“================================================================================\n“);
size?=?getline(&line&lenfd);
strncpy(p1->songnamelinestrlen(line));
head?=?p1;
while((size?=?getline(&line&lenfd))?!=?-1)
{
p2?=?p1;
p1?=?(struct?song?*)malloc(sizeof(struct?song));
strncpy(p1->songnamelinestrlen(line));
p2->next?=?p1;
p1->prev?=?p2;
}
p1->next?=?head;
head->prev?=?p1;
p1?=?NULL;
p2?=?NULL;
system(“rm?-rf?song_list“);
return?head;
}
/*******
評論
共有 條評論