6

asoundlib と C を使用して、システムで使用可能なサウンド カードのリストをプログラムで取得する方法はありますか? と同じ情報で欲しい/proc/asound/cards

4

1 に答える 1

7

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から短縮されたコードです(各カードを開いて名前を読み取ることもできます)。

于 2012-06-06T13:32:52.007 に答える