在Linux上编程查找可用的声卡

6

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

1个回答

8
您可以使用 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 中精简而来(该代码还会打开每张卡牌并读取其名称)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接