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:开一个子进程在子进程中:2:在父进程中:
启动Mplayer,参数规定通过命名管道进行通信;
把子进程的标准输出重定向无名管道的写端;
Mplayer从命名管道读到主进程发送的命令;
Mplayer发出的内容发送到无名管道中,父进程通过读管道就可以读到Mplayer发出的信息。
启动两个线程
第一个线程,不断使用fgets从键盘获取一个字符串命令,并写入命名管道中
第二个线程,循环检测无名管道是否有信息可读,有信息将其打印输出在屏幕上
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- /**********************全局变量定义区*****************/
- int fd_fifo; //创建有名管道,用于向mplayer发送命令
- int fd_pipe[2]; //创建无名管道,用于从mplayer读取命令
- void *get_pthread(void *arg)
- {
- char buf[100];
- while(1)
- {
- printf("please input you cmd:");
- fflush(stdout);
- fgets(buf,sizeof(buf),stdin); //从标准输入获取数据
- buf[strlen(buf)]='\0';
- printf("*%s*\n",buf);
- if(write(fd_fifo,buf,strlen(buf))!=strlen(buf))
- perror("write"); //将命令写入命名管道
- }
- }
- void *print_pthread(void *arg)
- {
- char buf[100];
- close(fd_pipe[1]);
- int size=0;
- while(1)
- {
- size=read(fd_pipe[0],buf,sizeof(buf)); //从无名管道的写端读取信息打印在屏幕上
- buf[size]='\0';
- printf("th msg read form pipe is %s\n",buf);
- }
- }
- int main(int argc, char *argv[])
- {
- int fd;
- char buf[100];
- pid_t pid;
- unlink("/tmp/my_fifo"); //如果明明管道存在,则先删除
- mkfifo("/tmp/my_fifo",O_CREAT|0666);
- perror("mkfifo");
- if (pipe(fd_pipe)<0 ) //创建无名管道
- {
- perror("pipe error\n");
- exit(-1);
- }
- pid=fork();
- if(pid<0)
- {
- perror("fork");
- }
- if(pid==0) //子进程播放mplayer
- {
- close(fd_pipe[0]);
- dup2(fd_pipe[1],1); //将子进程的标准输出重定向到管道的写端
- fd_fifo=open("/tmp/my_fifo",O_RDWR);
- execlp("mplayer","mplayer","-slave","-quiet","-input","file=/tmp/my_fifo","juhuatai.mpg",NULL);
- }
- else
- {
- pthread_t tid1;
- pthread_t tid2;
- fd_fifo=open("/tmp/my_fifo",O_RDWR);
- if(fd<0)
- perror("open");
- pthread_create(&tid1,NULL,get_pthread,NULL); //从键盘获取控制信息
- pthread_create(&tid2,NULL,print_pthread,NULL); //打印从无名管道收到的信息
- pthread_join(tid1,NULL);
- pthread_join(tid2,NULL);
- }
- return 0;
- }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
/**********************全局变量定义区*****************/
int fd_fifo; //创建有名管道,用于向mplayer发送命令
int fd_pipe[2]; //创建无名管道,用于从mplayer读取命令
void *get_pthread(void *arg)
{
char buf[100];
while(1)
{
printf("please input you cmd:");
fflush(stdout);
fgets(buf,sizeof(buf),stdin); //从标准输入获取数据
buf[strlen(buf)]='\0';
printf("*%s*\n",buf);
if(write(fd_fifo,buf,strlen(buf))!=strlen(buf))
perror("write"); //将命令写入命名管道
}
}
void *print_pthread(void *arg)
{
char buf[100];
close(fd_pipe[1]);
int size=0;
while(1)
{
size=read(fd_pipe[0],buf,sizeof(buf)); //从无名管道的写端读取信息打印在屏幕上
buf[size]='\0';
printf("th msg read form pipe is %s\n",buf);
}
}
int main(int argc, char *argv[])
{
int fd;
char buf[100];
pid_t pid;
unlink("/tmp/my_fifo"); //如果明明管道存在,则先删除
mkfifo("/tmp/my_fifo",O_CREAT|0666);
perror("mkfifo");
if (pipe(fd_pipe)<0 ) //创建无名管道
{
perror("pipe error\n");
exit(-1);
}
pid=fork();
if(pid<0)
{
perror("fork");
}
if(pid==0) //子进程播放mplayer
{
close(fd_pipe[0]);
dup2(fd_pipe[1],1); //将子进程的标准输出重定向到管道的写端
fd_fifo=open("/tmp/my_fifo",O_RDWR);
execlp("mplayer","mplayer","-slave","-quiet","-input","file=/tmp/my_fifo","juhuatai.mpg",NULL);
}
else
{
pthread_t tid1;
pthread_t tid2;
fd_fifo=open("/tmp/my_fifo",O_RDWR);
if(fd<0)
perror("open");
pthread_create(&tid1,NULL,get_pthread,NULL); //从键盘获取控制信息
pthread_create(&tid2,NULL,print_pthread,NULL); //打印从无名管道收到的信息
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
}
return 0;
}