最近使用树莓派的音频播放音频文件(需要外接声卡),自己在网上找一些alsa编程的代码用起来比较复杂,可以是自己设置的原因把,播放时有时会出现杂音。不过这两天看到了一个开源软件mplayer,它的slave模式,可以让你在通过FIFO文件控制它的播放停止和其他功能。这样你就可以通过程序控制mplayer了,甚至可以在它的基础上开发新的软件。这里用的是C语言。今天先立个搞,明天再更。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#define FIFO "/tmp/myfifo"
int main()
{
char * path = "./test.mp3";
if(mkfifo("/tmp/myfifo",0777))
printf("fifo create error\n");
if(!fork())
{
system("mplayer -slave -quiet -input file=/tmp/myfifo ./test.mp3");
exit(0);
}
else
{
sleep(10);
int fd = open(FIFO, O_WRONLY);
write(fd, "pause\n",strlen("pause\n"));
close(fd);
}
printf("end!\n");
return 0;
}
原文:http://my.oschina.net/u/2255341/blog/514001