Finding available sound cards on Linux programmatically

耗尽温柔 提交于 2019-12-23 09:27:29

问题


Is there a way to get a list of available sound cards on the system programmatically using asoundlib and C? I want it with the same information as /proc/asound/cards.


回答1:


You can iterate over the cards using snd_card_next, starting with a value of -1 to get the 0th card.

Here's sample code; compile it with 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();
}

This is code reduced from cardnames.c (which also opens each card to read its name).



来源:https://stackoverflow.com/questions/7288782/finding-available-sound-cards-on-linux-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!