歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> MPlayer從模式說明

MPlayer從模式說明

日期:2017/3/1 11:15:31   编辑:Linux編程
slave模式協議

一、簡介:

默認mplayer是從鍵盤上獲得控制信息

mplayer另外提供了一種更為靈活的控制方式,用來進行播放控制——slave模式

在slave模式下,MPlayer為後台運行其他程序,不再截獲鍵盤事件,

MPlayer會從標准輸入讀一個換行符(\n)分隔開的命令。


二、操作:
#mplayer -input cmdlist

//會打印出一份當前mplayer所支持的所有slave模式的命令

方法一:從控制台輸入控制命令(測試使用)
運行mplayer -slave -quiet <movie>,並在控制台窗口輸入slave命令。
//-slave 啟動從模式
//-quiet 不輸出冗余的信息

常用到的 Mplayer指令:
loadfile string //參數string 為 歌曲名字。
volume 100 1//設置音量 中間的為音量的大小。
mute1/0//靜音開關
pause//暫停/取消暫停
get_time_length//返回值是播放文件的長度,以秒為單位。
seek value //向前查找到文件的位置播放 參數value為秒數。
get_percent_pos//返回文件的百分比(0--100)
get_time_pos//打印出在文件的當前位置用秒表示,采用浮點數
volume <value> [abs] //增大/減小音量,或將其設置為<value>,如果[abs]不為零
get_file_name//打印出當前文件名
get_meta_album//打印出當前文件的'專輯'的元數據
get_meta_artist//打印出當前文件的'藝術家'的元數據
get_meta_comment//打印出當前文件的'評論'的元數據
get_meta_genre//打印出當前文件的'流派'的元數據
get_meta_title//打印出當前文件的'標題'的元數據
get_meta_year//打印出當前文件的'年份'的元數據


方法二:從有名管道(fifo)輸入控制命令(應用編程中使用)

#mkfifo </tmp/fifofile>

#mplayer -slave -input file=</tmp/fifofile> <movie>

//用戶可以通過往管道裡寫入slave命令來實現對應的功能

例:主進程創建一個無名管道和一個有名管道

1:開一個子進程
在子進程中:
啟動Mplayer,參數規定通過命名管道進行通信;
把子進程的標准輸出重定向無名管道的寫端;
Mplayer從命名管道讀到主進程發送的命令;
Mplayer發出的內容發送到無名管道中,父進程通過讀管道就可以讀到Mplayer發出的信息。
2:在父進程中:
啟動兩個線程
第一個線程,不斷使用fgets從鍵盤獲取一個字符串命令,並寫入命名管道中
第二個線程,循環檢測無名管道是否有信息可讀,有信息將其打印輸出在屏幕上
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <string.h>
  8. /**********************全局變量定義區*****************/
  9. int fd_fifo; //創建有名管道,用於向mplayer發送命令
  10. int fd_pipe[2]; //創建無名管道,用於從mplayer讀取命令
  11. void *get_pthread(void *arg)
  12. {
  13. char buf[100];
  14. while(1)
  15. {
  16. printf("please input you cmd:");
  17. fflush(stdout);
  18. fgets(buf,sizeof(buf),stdin); //從標准輸入獲取數據
  19. buf[strlen(buf)]='\0';
  20. printf("*%s*\n",buf);
  21. if(write(fd_fifo,buf,strlen(buf))!=strlen(buf))
  22. perror("write"); //將命令寫入命名管道
  23. }
  24. }
  25. void *print_pthread(void *arg)
  26. {
  27. char buf[100];
  28. close(fd_pipe[1]);
  29. int size=0;
  30. while(1)
  31. {
  32. size=read(fd_pipe[0],buf,sizeof(buf)); //從無名管道的寫端讀取信息打印在屏幕上
  33. buf[size]='\0';
  34. printf("th msg read form pipe is %s\n",buf);
  35. }
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. int fd;
  40. char buf[100];
  41. pid_t pid;
  42. unlink("/tmp/my_fifo"); //如果明明管道存在,則先刪除
  43. mkfifo("/tmp/my_fifo",O_CREAT|0666);
  44. perror("mkfifo");
  45. if (pipe(fd_pipe)<0 ) //創建無名管道
  46. {
  47. perror("pipe error\n");
  48. exit(-1);
  49. }
  50. pid=fork();
  51. if(pid<0)
  52. {
  53. perror("fork");
  54. }
  55. if(pid==0) //子進程播放mplayer
  56. {
  57. close(fd_pipe[0]);
  58. dup2(fd_pipe[1],1); //將子進程的標准輸出重定向到管道的寫端
  59. fd_fifo=open("/tmp/my_fifo",O_RDWR);
  60. execlp("mplayer","mplayer","-slave","-quiet","-input","file=/tmp/my_fifo","juhuatai.mpg",NULL);
  61. }
  62. else
  63. {
  64. pthread_t tid1;
  65. pthread_t tid2;
  66. fd_fifo=open("/tmp/my_fifo",O_RDWR);
  67. if(fd<0)
  68. perror("open");
  69. pthread_create(&tid1,NULL,get_pthread,NULL); //從鍵盤獲取控制信息
  70. pthread_create(&tid2,NULL,print_pthread,NULL); //打印從無名管道收到的信息
  71. pthread_join(tid1,NULL);
  72. pthread_join(tid2,NULL);
  73. }
  74. return 0;
  75. }
Copyright © Linux教程網 All Rights Reserved