问题描述
我目前正在尝试使用 pulseaudio 简单 API 来录制麦克风来自我的 USB 声卡和树莓派 3 的数据.我使用了示例程序 parec-simple 来自我自己程序中的pulseaudio,效果很好.
I am currently trying to use the pulseaudio simple API to record microphone data from my USB sound card with my raspberry pi 3. I used the example program parec-simple from pulseaudio in my own program and it works quite nice.
我使用此代码的程序正在访问 gpio,因此我需要以 root 身份运行它.但是,当我尝试以 root 身份执行程序时,出现以下错误:
The program i used this code for is accessing gpio's so i need to run this as root. However, when i try to execute the program as root, i get the following errors:
Home directory not accessible: Permission denied
W: [pulseaudio] core-util.c: Failed to open configuration file '/root/.config/pulse//daemon.conf': Permission denied
W: [pulseaudio] daemon-conf.c: Failed to open configuration file: Permission denied
pa_simple_new() failed: Connection refused
使用的代码如下:
static const pa_sample_spec ss = {
.format = PA_SAMPLE_S16LE,
.rate = 44100,
.channels = 1
};
pa_simple *s = NULL;
int ret = 1;
int error;
/* Create the recording stream */
if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
fprintf(stderr, "pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
}
while(1)
{
uint8_t buf[BUFSIZE];
/* Record some data ... */
if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
goto finish;
}
/* And write it to STDOUT */
if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
goto finish;
}
}
ret = 0;
finish:
if (s)
pa_simple_free(s);
return ret;
我已经按照此处的建议尝试了chown pi:pi/home/pi
尝试修复它,但它不起作用.将/home/pi 的所有者从 pi 更改为 root 对我也不起作用.
I already tried chown pi:pi /home/pi
as suggested here to try to fix it but it doesn't work. changing the owner of /home/pi from pi to root didn't work for me either.
我也尝试干净地重新安装pulseaudio,但不幸的是它没有修复它.
I also tried a clean reinstall of pulseaudio but unfortunately it didn't fix it.
那么我该怎么做才能修复这些错误?
So what can i do to fix these errors?
推荐答案
如果您需要以 root 用户身份运行您的程序,那么您必须模拟 root.我不知道 pulseaudio 是查看用户名以查找配置文件,还是查看 $HOME 变量.在第二种情况下,也许将 HOME 设置为工作"用户的家会有所帮助.
If you need to run your program as user root, then you must impersonate root. I don't know if pulseaudio looks at the username in order to find configuration files, or it looks at the $HOME variable. In the second case, maybe that by setting HOME to the home of a "working" user helps.
不管怎样,你说的情况很清楚:pulseaudio没有找到文件:
Anyway what you told about the situation is clear: pulseaudio does not find a file:
'/root/.config/pulse//daemon.conf'
在那个目录中放置一个正确的daemon.conf"——也许你可以从某个地方复制它(比如/home/auser/.config/pulse/daemon.conf).
Place a correct "daemon.conf" in that directory - probably you can copy it from somewhere (like /home/auser/.config/pulse/daemon.conf).
考虑到名称以点开头的目录通常是隐藏的;如果使用文件管理器,您必须启用显示隐藏文件",如果您使用 shell,ls -a
可以提供帮助.
Consider that directories with name starting with a dot are normally hidden; if using a file manager you must enable "show hidden files", if you use the shell, ls -a
can help.
你的第一个目标是确认文件在那里,你的程序不应该抱怨丢失/不可读的配置文件.然后,也许会出现其他错误,但您可以一个接一个地消除它们.
Your first target is to confirm that the file is there, and your program should not complain about a missing/unreadable config file. Then, maybe other errors will show up but, one after another, you can eliminate them.
这篇关于如何以 root 身份使用pulseaudio API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!