linux下编程实现mplayer播放器主文件main.c

linux下编程实现mplayer播放器主文件main.c

这是我写该项目的主文件,我贴上来主要是自己以后有时间了回头看看,还有自己刚开始学习linux,肯定有好多编程问题,希望好心人能指出。

主文件mplayer_main.c

  1. /* ************************************************************************  
  2. //                                  Last Change:  2011-01-19 15:56:14  
  3.  *       Filename:  mplayer_main.c  
  4.  *    Description:  mplayer maintance file  
  5.  *        Version:  1.0  
  6.  *        Created:  2011-1-13 11:27:45  
  7.  *       Revision:  none  
  8.  *       Compiler:  gcc  
  9.  *         Author:  panda   
  10.  *        Company:  sunplusapp   
  11.  * ************************************************************************/   
  12. #include"mplayer_include.h"   
  13. #define FIFO    "fifo"   
  14. int  fd;  
  15. int  pipedes[2];  
  16. char  msg_buf[REC_MSG_CHNUM ] ; /*接收到信息缓冲区*/   
  17. int  my_lock=0; //发送命令标志位   
  18. /*****************************************************************************  
  19.  *       Function name: send_cmd  
  20.  *         Description: send commend to mplayer by fifo  
  21.  *    parameter  input: cmd  
  22.  *    parameter output: None  
  23.  *              return: None  
  24.  *****************************************************************************/   
  25. void  send_cmd( char  *cmd) //通过有名管道向mplayer发送命令   
  26. {  
  27.     if ((write(fd,cmd,strlen(cmd)))!=strlen(cmd))  
  28.     {  
  29.         perror("write cmd" );  
  30.     }  
  31. }  
  32. /*****************************************************************************  
  33.  *       Function name: touch_pthread  
  34.  *         Description: a pthread that checking toucher status all times   
  35.  *    parameter  input: None  
  36.  *    parameter output: None  
  37.  *              return: None  
  38.  *****************************************************************************/   
  39. void  *touch_pthread()  
  40. {  
  41.     int  key=0;  
  42.     while (1)  
  43.     {  
  44.         usleep(100*MS);//延时处理触摸屏键值   
  45.         if (ts_read(&ts)==-1) //读取按下的点   
  46.             continue ;  
  47.         if ((key=Touch_Trans(ts.x,ts.y))==-1) //将按下的点转化成设定的键值   
  48.             continue ;  
  49.         key_dispose(key);//处理键值   
  50.     }  
  51.     return  NULL;  
  52. }  
  53. /*****************************************************************************  
  54.  *       Function name: pipe_read_pthread  
  55.  *         Description: a pthread that ciculate reading msg from pipe data that reciver to mplayer  
  56.  *    parameter  input: None  
  57.  *    parameter output: None  
  58.  *              return: None  
  59.  *****************************************************************************/   
  60. void  *pipe_read_pthread()  
  61. {  
  62.     int  size;  
  63.     char  buf[REC_MSG_CHNUM];  
  64.     while (1)  
  65.     {  
  66.         memset(buf, 0 , REC_MSG_CHNUM) ;  
  67.         if ((size = read(pipedes[0],buf, sizeof (buf))) == -1) //读取mplayer发过来的歌曲信息   
  68.         {  
  69.             perror("read pipe" );  
  70.             exit(1);  
  71.         }  
  72.         if ( size == 0) //如果没有读到信息,则返回继续读取   
  73.             continue ;  
  74.         buf[size]='/0' ; //使信息变成字符串,便于处理   
  75. //      printf("******************msg_buf=%s/n/n",buf);   
  76.         strcpy(msg_buf,buf);  
  77.        if (strncmp(buf, "ANS_META" ,8) ==0)                 //获取歌曲信息   
  78.         {  
  79.             buf[strlen(buf)-2]='/0' ; //多减一个去掉引号   
  80.             msg_dispose(buf);  
  81.         }  
  82.         sem_post(&cmd_sem) ;  
  83.     }  
  84.     return  NULL;  
  85. }  
  86. /*****************************************************************************  
  87.  *       Function name: pipe_read_dispose_pthread  
  88.  *         Description: a pthread that ciculate resolving msg  
  89.  *    parameter  input: None  
  90.  *    parameter output: None  
  91.  *              return: None  
  92.  *****************************************************************************/   
  93. void  *pipe_read_dispose_pthread()  
  94. {  
  95.     char  buf[REC_MSG_CHNUM];  
  96.     while (1)  
  97.     {  
  98.         sem_wait(&cmd_sem) ;  
  99.         strcpy(buf,msg_buf);  
  100.         if (strncmp(buf, "ANS_PERCENT_POSITION" , 20)==0)       //获取进度信息   
  101.         {  
  102.             percent_dispose(buf);     
  103.         }  
  104.         else   if (strncmp(buf, "ANS_TIME_POSITION" , 17) ==0)    //获取歌曲当前播放时间   
  105.         {  
  106.             time_dispose(buf);  
  107.         }  
  108.   
  109.         else   if (strncmp(buf, "ANS_LENGTH" ,10) ==0)            //获得歌的总长度   
  110.         {  
  111.             length_dispose(buf);  
  112.         }  
  113.     }  
  114.     return  NULL;  
  115. }  
  116.   
  117. /*****************************************************************************  
  118.  *       Function name: get_percent_pos_pthread  
  119.  *         Description: a pthread that geting song time and percent of mplayer  
  120.  *    parameter  input: None  
  121.  *    parameter output: None  
  122.  *              return: None  
  123.  *****************************************************************************/   
  124. void  *get_pos_pthread()  
  125. {  
  126.     while (1)  
  127.     {  
  128.         if (my_lock==1 )  
  129.         {  
  130.             usleep(500*MS);  
  131.             send_cmd("get_percent_pos/n" );  
  132.             usleep(500*MS);  
  133.             send_cmd("get_time_pos/n" );  
  134.         }   
  135.     }  
  136.     return  NULL;  
  137. }  
  138. /*****************************************************************************  
  139.  *       Function name: display_lrc_pthread  
  140.  *         Description: a pthread that displaying lyric  
  141.  *    parameter  input: None  
  142.  *    parameter output: None  
  143.  *              return: None  
  144.  *****************************************************************************/   
  145. void  *display_lrc_pthread()  
  146. {  
  147.     LRC *temp;  
  148.     char  oldtime[10]= "00:00" ;  
  149.     sleep(1);  
  150.     while (1)  
  151.     {  
  152.         if (strcmp(song_msg.cur_time,oldtime)!=0) //时间变化了进入相应的歌词显示   
  153.         {  
  154.             temp=head;  
  155.             while (temp!=NULL)  
  156.             {  
  157.                 if (display_lrc(temp)==1) //如果找到该时间的歌词,则跳出循环,等待下一次时间的到来   
  158.                 {  
  159.                     break ;    
  160.                 }  
  161.                 temp=temp->next;  
  162.             }  
  163.             strcpy(oldtime, song_msg.cur_time);  
  164.         }  
  165.     }  
  166.     free_link(head);//释放链表空间   
  167.     return  NULL;  
  168. }  
  169. /*****************************************************************************  
  170.  *       Function name: main  
  171.  *         Description: maintance function   
  172.  *    parameter  input: None  
  173.  *    parameter output: None  
  174.  *              return: None  
  175.  *****************************************************************************/   
  176. int  main( int  argc, char  *argv[])  
  177. {  
  178.     pid_t pid;  
  179.       
  180.     unlink(FIFO);//如果管道存在,先删除   
  181.     tft_init();//初始化触摸屏   
  182.     get_song_list();//得到歌曲列表   
  183.   
  184.         unlink(FIFO);//如果管道存在,先删除   
  185.     if (mkfifo( "fifo" ,IPC_CREAT|0x744)==-1) //创建有名管道   
  186.     {  
  187.         perror("mkfifo" );  
  188.         exit(1);  
  189.     }  
  190.       
  191.     if (pipe(pipedes)==-1) //创建无名管道用于从mplayer读取歌曲信息   
  192.     {  
  193.         perror("pipe" );  
  194.         exit(1);  
  195.     }  
  196.   
  197.     if ((pid=fork())==-1)  
  198.     {  
  199.         perror("fork" );  
  200.         exit(1);  
  201.     }  
  202.     else   if (pid==0) //在子进程中播放歌曲   
  203.     {  
  204.         char  song[SONG_CHNUM];    
  205.         close(pipedes[0]);  
  206.         dup2(pipedes[1],1);  
  207.         sprintf(song,"%s%s" , "./song/" ,song_list[0]); //得到整个歌曲路径   
  208.         execlp("./mplayer" , "" , "-ac" , "mad" , "-slave" , "-quiet" , "-input" , "file=fifo" ,song,NULL);  
  209.           
  210.     }  
  211.     else   if (pid>0)  
  212.     {  
  213.         pthread_t tid_touch;  
  214.         pthread_t tid_pr;  
  215.         pthread_t tid_prd;  
  216.         pthread_t tid_gpp;  
  217.         pthread_t tid_plp;  
  218.   
  219.         usleep(500*MS);//等待让歌曲播放之后在获取信息   
  220.         if ((fd=open(FIFO,O_RDWR))==-1)  
  221.         {  
  222.             perror("open" );  
  223.             exit(1);  
  224.         }  
  225.   
  226.         pthread_create(&tid_touch,NULL,touch_pthread,NULL);//检测触摸屏   
  227.           
  228. /*循环读管道把读到的消息保存在字符数组中*/   
  229.         pthread_create(&tid_pr,NULL,pipe_read_pthread,NULL);  
  230.   
  231. /*解析读到的消息,把有用的消息解析出来,做相应的处理*/   
  232.         pthread_create(&tid_pr,NULL,pipe_read_dispose_pthread,NULL);  
  233.   
  234. /*每隔一段时间发一条检测时间的命令,获取当前播放时间*/   
  235.         pthread_create(&tid_gpp,NULL,get_pos_pthread,NULL);  
  236.         pthread_create(&tid_plp,NULL,display_lrc_pthread,NULL);//显示歌词   
  237.           
  238.         slice_song();     //切歌后要执行的函数   
  239.                   
  240.         pthread_join(tid_touch,NULL);  
  241.         pthread_join(tid_pr,NULL);  
  242.         pthread_join(tid_prd,NULL);  
  243.         pthread_join(tid_gpp,NULL);  
  244.         pthread_join(tid_plp,NULL);  
  245.     }  
  246.       
  247.     return  0;  
  248. }  

总的头文件mplayer_include.h


来至:http://blog.csdn.net/panda19881/archive/2011/01/19/6152855.aspx
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值