加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:03:27 所属栏目:Linux 来源:网络整理
导读:有没有办法使用asoundlib和C以编程方式获取系统上的可用声卡列表?我希望它与/ proc / asound / cards具有相同的信息. 解决方法 您可以使用snd_card_next迭代卡片,从值-1开始以获得第0张卡片. 这是示例代码;用gcc -o countcards countcards.c -lasound编译它
有没有办法使用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: %sn",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减少的代码(也打开每张卡以读取其名称).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读