c - 以编程方式在 Linux 上查找可用的声卡

标签 c linux alsa

有没有办法使用 asoundlib 和 C 以编程方式获取系统上可用声卡的列表?我希望它具有与 /proc/asound/cards 相同的信息。

最佳答案

您可以使用 snd_card_next 遍历卡片,从值 -1 开始获取第 0 张卡片。

这是示例代码;用 gcc -o countcards countcards.c -lasound 编译:

#include <alsa/asoundlib.h>
#include <stdio.h>

int main()
{
    int totalCards = 0;   // No cards found yet
    int cardNum = -1;     // Start with first card
    int err;

    for (;;) {
        // Get next sound card's card number.
        if ((err = snd_card_next(&cardNum)) < 0) {
            fprintf(stderr, "Can't get the next card number: %s\n",
                            snd_strerror(err));
            break;
        }

        if (cardNum < 0)
            // No more cards
            break;

        ++totalCards;   // Another card found, so bump the count
    }

    printf("ALSA found %i card(s)\n", totalCards);

    // ALSA allocates some memory to load its config file when we call
    // snd_card_next. Now that we're done getting the info, tell ALSA
    // to unload the info and release the memory.
    snd_config_update_free_global();
}

这是从 cardnames.c 减少的代码(它还会打开每张卡片以阅读其名称)。

关于c - 以编程方式在 Linux 上查找可用的声卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7288782/

相关文章:

linux - 使用 ALSA [或 V4L2] 通过轮询读取麦克风数据

android - 有什么方法可以通过tinymix命令捕获音频吗?

C ArrayList 崩溃

java - Netty WebSocket 客户端 channel 在 Linux 服务器上总是处于非 Activity 状态

c - NULL 和 0 在 C 中是否完全等价?

c - 我在这里删除堆栈吗?

c - 等到 2 pid 写入 FIFO(命名管道)

audio - 如何使用gst-launch播放原始音频文件?

c++ - 过时 - OpenCV 的错误模式

c - ELF文件的哪些部分应该加载到内存中?