【发布时间】:2016-10-24 19:46:06
【问题描述】:
我必须在 linux 上用 C 语言编写程序,可以使用 mknod() 函数控制 mplayer。
当我通过这个命令运行 mplayer 时
mplayer -input file=/tmp/film spiderman.ts
我想用我的 C 程序来控制它,就像使用 echo 函数一样
echo "pause" >> /tmp/film
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
int main(int argc, char* argv[]){
int fdes,res;
char x;
/*commands*/
char msg1[] = "pause\n";
char msg2[] = "quit\n";
/*creating fifo file*/
unlink("/tmp/film");
res=mknod("/tmp/film", S_IFIFO|0666, 0);
if (res<0) perror("error_creating_fifo");
fdes = open("/tmp/film", O_WRONLY);
if (fdes<0) perror("error_open_fifo");
while(1)
{
printf("Enter command\n");
x = getchar();
getchar();//dont take enter character
switch(x)
{
case 'p':
printf("PAUSE\n");
write(fdes, msg1, sizeof(msg1));
break;
case 'q':
printf("QUIT\n");
write(fdes, msg2, sizeof(msg2));
break;
default:
printf("Unknown command");
break;
}
}
close(fdes);
return 0;
}
问题是,它只能工作一次。例如,我不能暂停然后取消暂停电影。
【问题讨论】:
-
mplayer不是已经有合适的控制界面了吗?甚至是 dbus?
-
fdes 从未初始化。也许您打算在其他地方使用 res ?
-
是的,我在准备发布正确问题的代码时忘记粘贴 fdes 初始化。已修复:)
-
您解决了这个问题吗?您是否可能必须为每个命令关闭和打开管道?如果你运行
echo "pause" >> /tmp/film,管道将在回显完成后关闭,mplayer 会看到它关闭。您可以通过运行cat /tmp/film而不是 mplayer 来测试这一点,然后运行 echo 命令,cat 将打印“暂停”然后退出。可能 mplayer 正在寻找命令之间管道的这个 close。