4

我正在尝试获取 OpenBSD 中 C 程序的音量等音频信息。通过 shell 命令将是

mixerctl outputs.master

但是我怎样才能在 C 中得到它呢?到目前为止,我只在audio(4)手册页中找到了类似的东西,但我无法让它工作(我不擅长 C):

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    audio_info_t *info;
    int fd;

    fd = fopen("/dev/audioctl", "r");
    if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
        fprintf(stderr, "%s\n", strerror(errno));
    ...
}

给我Inappropriate ioctl for device。我究竟做错了什么?这是获取音量的正确方法吗?

解决方案:

我的错误似乎是错误地打开文件和移交info变量的混合。两者都植根于我对指针感到困惑......这是我如何让它工作的:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    audio_info_t info;
    FILE *fd;

    fd = fopen("/dev/audioctl", "r");
    if (ioctl(fileno(fd), AUDIO_GETINFO, &info) < 0)
        fprintf(stderr, "%s\n", strerror(errno));

    printf("%d", info.play.gain);
    fclose(fd);
}
4

0 回答 0