資源簡介
居于linux的音頻播放和停止,可以作為項目使用

代碼片段和文件信息
/*
?*?????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號*/
static?pid_t?gradchild;
/*子進程id號*/
/*共享內存描述標記*/
int?shmid;
pid_t?pid;
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)
{
#if?1
/*把歌曲名加上根路徑*/
strcat(my_song?currentsong->songname);
p?=?my_song;
len?=?strlen(p);
/*去掉文件名最后的‘\n‘*/
my_song[len-1]=‘\0‘;
printf(“THIS?SONG?IS?%s\n“?my_song);
#endif?
//system(“aplay?makerror.wav“);
execl(“/usr/bin/aplay“?“aplay“?my_song?NULL);
sleep(1);
printf(“\n\n\n“);
}
else
{
/*內存映射*/
c_addr?=?shmat(shmid?0?0);
/*把孫子進程的id和當前播放歌曲的節點指針傳入共享內存*/
memcpy(c_addr?&fd?sizeof(pid_t));
printf(“fd?=?%d\n“?fd);
memcpy(c_addr?+?sizeof(pid_t)+1?¤tsong?4);
memcpy(&gradchild?&fd?sizeof(pid_t));
/*把孫子進程的pid傳給父進程*/
/*使用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
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????8440??2013-12-31?13:12??app實現音樂播放?暫停\play_app.c
?????目錄???????????0??2014-02-13?23:04??app實現音樂播放?暫停\
- 上一篇:SLR1語法分析生成器
- 下一篇:自己制作卡拉OK歌曲
評論
共有 條評論