首页 文章

找到我可以使用ALSA播放PCM的所有设备

提问于
浏览
4

我使用ALSA播放PCM样本 . 我用这个函数打开PCM流:

int snd_pcm_open(snd_pcm_t** pcmp,
        const char* name,
        snd_pcm_stream_t stream,
        int mode);

我目前正在使用“default”作为名称参数 . 我希望能够选择其他设备 . 我无法理解的是如何确定其他可用设备的名称 .

我将USB麦克风连接到我的系统,aplay和amixer似乎检测到了新设备 . 如何确定该设备的名称?是否有任何ALSA功能可以获取具有各自名称的可用设备列表?

2 回答

  • 7

    我想你可以使用snd_device_name_hint来枚举设备 . 这是一个例子 . 请注意,我还没有编译它!

    char **hints;
    /* Enumerate sound devices */
    int err = snd_device_name_hint(-1, "pcm", (void***)&hints);
    if (err != 0)
       return;//Error! Just return
    
    char** n = hints;
    while (*n != NULL) {
    
        char *name = snd_device_name_get_hint(*n, "NAME");
    
        if (name != NULL && 0 != strcmp("null", name)) {
            //Copy name to another buffer and then free it
            free(name);
        }
        n++;
    }//End of while
    
    //Free hint buffer too
    snd_device_name_free_hint((void**)hints);
    
  • 2

    这是我对linux / unix项目的第一个要求,我需要知道所有可用的音频设备功能和名称 . 然后我需要使用这些设备来捕获和回放音频 . 我所做的很简单 . 有一个linux / unix命令用于通过linux中的alsa实用程序查找设备 .

    它是:

    aplay -l
    

    现在我所做的只是制作一个程序,像alsa一样给出这个 .

    为了大家的帮助,我创建了一个(.so)库和一个示例应用程序来演示在c中使用这个库 .

    我的图书馆的输出像 -

    [root@~]# ./IdeaAudioEngineTest
    HDA Intel plughw:0,0
    HDA Intel plughw:0,2
    USB Audio Device plughw:1,0
    

    该库还可以捕获和回放实时音频数据 .

    它可以在IdeaAudio library with Duplex Alsa Audio中找到文档

相关问题