以编程方式在Linux上find可用的声卡

有没有办法通过使用asoundlib和C编程获取系统上可用的声卡列表? 我想要它与/proc/asound/cards相同的信息。

你可以使用snd_card_next迭代卡片,从-1开始获得第0张卡片。

这里是示例代码; 使用gcc -o countcards countcards.c -lasound编译它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 (这也打开每个卡读取其名称)的代码减少。